I was studying sockets for this material link and I came across a code that I do not understand what is going on. The code part is this:
struct hostent *ip;
...
// função da lib netdb.h para converter ai nosso host para IP
ip = gethostbyname(host);
url=inet_ntoa(*(struct in_addr *)ip->h_addr);
strcat(getpage,page);
printf("%s",getpage);
The line that starts with 'url' is that I do not understand what's going on. From what I've researched so far I've found that: the struct hostent has the fields
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
and the h_addr
field equals
char **h_addr_list[0]; //primeira posiçao do vetor h_addr_list
.
gethostbyname()
returns a hostent
(struct!) for the 'ip' variable, then accessing ip-> h_addr I'm accessing a pointer that is of type char
, but is being cast for pointer of struct in_addr
:
url=inet_ntoa(*(struct in_addr *)ip->h_addr);
but h_addr
is not a struct in_addr (which has an unsigned long) and to complete, this asterisk * that comes before (struct in_addr *)
what are you doing? I did not understand this syntax ...
I researched a lot but still did not understand.