Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 02 Nov 2001 10:56:11 -0800 (PST)
From:      John Baldwin <jhb@FreeBSD.org>
To:        Fabio Miranda <fmirand@yahoo.com>
Cc:        freebsd-chat@FreeBSD.org
Subject:   RE: struct hostent
Message-ID:  <XFMail.011102105611.jhb@FreeBSD.org>
In-Reply-To: <20011102161519.40762.qmail@web11504.mail.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help

On 02-Nov-01 Fabio Miranda wrote:
> Hi, i am coding a small c program in order to learn
> network programming:
> main(int argc, char *argv[]){
> struct hostent *host;
> ....
> if((host=gethostbyname(argv[1])=NULL)){
>         perror("gethostbyname");
>         exit(-1);}

This should be:

if ((host = gethostbyname(argv[1])) == NULL) {

Note the "==" check for NULL.  You are assigning NULL to host effectively,
causing your core dump.  I would also persoally recommand a slightly different
style, but that is a matter of personal opinion I suppose.

> /* now, i want to print out all the hostent struct
> information */
> printf("struct hostent{\n");
> printf("char *h_name: %s", host->h_name);
> printf("char **h_aliases:%s\n", host->h_length);
> printf("int h_addrtype: %d\n", host->h_addrtype);
> printf("int h_length:   %d\n", host->h_length);
> printf("char **h_addr_list:  %d\n",
> host->h_addr_list);
> }
> }
> This program core dumped, it may crash because i am
> trying to print an char array (h_addr_list, right?), I
> want to know how can i print all the information store
> in a struct hostent?, especiality the ip address.
> 
> thanks
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Find a job, post your resume.
> http://careers.yahoo.com
> 
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-chat" in the body of the message

-- 

John Baldwin <jhb@FreeBSD.org> -- http://www.FreeBSD.org/~jhb/
PGP Key: http://www.baldwin.cx/~john/pgpkey.asc
"Power Users Use the Power to Serve!"  -  http://www.FreeBSD.org/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-chat" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.011102105611.jhb>