Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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?

Explanation / Answer

Dear, Arrays are used to store similar type of datatypes in consecutive memory locations, but the main drawback in arrays is the size of the array cannot be incresed or changed at the runtime that is dynamically. Some how we use double dimension pointers to store the character type values and to increase the size of memory to store the enough values. When program starts executing the character arrays or pointers are initially filled with grabage values that is NULL, if we set any values to the array elements then the garbage values are reaplced, so if we keep track the elements in the arrays we compare the array element with the NULL value, and hence it will be taken as sentinal value that is terminating value.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote