Reputation: 59
I am trying to print some output in my C program.
When I compile with cc, I get this error:
"whoisclient.c", line 70: argument #1 is incompatible with prototype: prototype: struct in_addr {union {..} S_un} : "/usr/include/arpa/inet.h", line 61 argument : pointer to char
At this part of my code:
printf("Addresses\n");
int j=0;
while(hp ->h_addr_list[j]!=NULL){
printf("hp_addr_list[%d] = %s\n", j, inet_ntoa(hp->h_addr_list[j]));
j++;
}
I have tried adding an asterisk or adding / removing hp->
but I am out of ideas. I don't fully understand the error message and what it is saying I am doing wrong.
Upvotes: 0
Views: 446
Reputation: 223897
The inet_ntoa
function takes a struct in_addr
, which contains a numerical representation of an IPv4 address, and returns a pointer to a string contains a text representation of that IP address.
The h_addr_list
field of a struct hostent
is a pointer to an array of pointers to strings, each of which contains an IPv4 address. This is incompatible with what inet_ntoa
is expecting.
Since you already have a string representation of an IP address and not a numerical representation, you don't need to pass it through inet_ntoa
to convert it. Just pass the string directly to printf
:
printf("hp_addr_list[%d] = %s\n", j, hp->h_addr_list[j]);
EDIT:
As mentioned in the comments, the h_addr_list
member, which is defined as type char **
is actually an array of pointers to network addresses in network byte order. Such an element is the same as the contents of a struct in_addr
. So the char *
needs to be casted to a struct in_addr *
and then dereferenced:
printf("hp_addr_list[%d] = %s\n", j, inet_ntoa(*((struct in_addr *)hp->h_addr_list[j])));
Upvotes: 3