4. In the web_client.c program (included in the ZIP), there\'s a call to gethost
ID: 3627211 • Letter: 4
Question
4. In the web_client.c program (included in the ZIP),there's a call to gethostbyname, which takes a string and returns a
pointer to a struct hostent. Here's a code segment to illustrate:
struct hostent* host_ptr = gethostbyname("condor.edu");
This pointer is passed to the function dump_host_aux, which prints
information about the host with the given name. In the case of
www.yahoo.com, for instance, here's the output:
Official name: www.yahoo.akadns.net
Aliases: www.yahoo.com
Host type: AF_INET
Address length: 4
Addresses: 68.142.197.86
68.142.197.87
68.142.197.90
68.142.197.67
68.142.197.69
68.142.197.75
68.142.197.77
68.142.197.84
The C structure itself looks like this:
struct hostent {
char* h_name; ;; official name
char** h_aliases; ;; alias list
int h_addrtype; ;; host address type
int h_length; ;; length of address
char** h_addr_list; ;; list of addresses
};
Of interest here is the second field in the structure, h_aliases, which
is a list of alternative names. (The official name for yahoo is not
www.yahoo.com but rather www.yahoo.akadns.net; hence, www.yahoo.com is an
alias for this official name.) Here's the loop that prints the aliases:
printf("Aliases: ");
int i = 0;
while (host_ptr->h_aliases[i] != NULL) {
printf("%.21s ", host_ptr->h_aliases[i]);
i++;
}
So the loop terminates when the ith entry is NULL, which is defined as 0
in the header file stdlib.h.
Why is the compiler willing to accept an array of strings that includes
an integer at the end?