It’s from here,but fails when compiling:
int main(int argc, char **argv)
{
struct hostent {
char *h_name; // main name
char **h_aliases; // alternative names (aliases)
int h_addrtype; // address type (usually AF_INET)
int h_length; // length of address (in octets)
char **h_addr_list; // alternate addresses (in Network Byte Order)
};
#define h_addr h_addr_list[0] // First address of h_addr_list.
struct hostent *info_stackoverflow;
int i = 0;
info_stackoverflow = gethostbyname( "www.stackoverflow.com" );
printf("The IP address of %s is %s",
info_stackoverflow->h_name,
inet_ntoa( * ((struct in_addr *)info_stackoverflow->h_addr )));
/* aliases */
while( *(pc_ip->h_aliases + i) != NULL )
{
printf("\n\tAlias: %s", *(pc_ip->h_aliases + i) );
i++;
}
}
Though
struct hostentis already defined by Winsock, so you will want to remove the definition ofhostentfrom your code snippet.As dmazzoni has noted,
pc_ipis not declared in that code. It is being used like a pointer to ahostentstructure, so you could probably replacepc_ipwithinfo_stackoverflow.When you link, you will need to link against
ws2_32.lib. At run-time, you’ll probably have problems until you add a call toWSAStartupat the beginning of your code, andWSACleanupat the end, before you return frommain.