Networking Question C/C++

Hi all,

I need to understand this point, so perhaps someone would be able to help. The following code:

1
2
3
4
5
6
7
struct hostent {
   char*    name;       
   char**   aliases;    
   int      addrtype;   
   int      length;     
   char**   addr_list;  
   };


is part of the C Library. Of this, I am interested in knowing
char** aliases;

points to an array of strings and uses NULL (or 0) as the last element in the list. If you put, -1 in place of NULL, you get a compiler warning. So my question is, why is the compiler willing to accept an array initialization in which the last element is not a string, but an integer?

Thanks
first of all it's not a string, it's a pointer and that's the key. It's a pointer to the start of an array of chars. A pointer is a memory address so it is effectively an int holding a number. setting a pointer to zero simply means it is pointing to memory address 0. This address has special significance and is regarded as a place holder and indicates the pointer has not been set, or in your case the end of a variable length array. If you try to modify the contents of memory at address zero, you program will bomb out.
bnbertha,

I see, but I still dont understand why the last element is an integer. My only understanding is that its a null terminator, but apart from that, what other significance does it have?

Thanks a lot.
From what I see, the last element IS an integer because it needs a null to terminate an array.

There's nothing else significant I can see.

POST 100!!1!!11!!1!one!!1!eleven!!
How do you know how many character arrays are stored in aliases?

You can't know. So if the last entry is going to be a null (0) then you can incrementally process until you hit the null. Then you know you have finished.
All of the elements are integers. Each integer is an address of a memory location where the first character of the string is stored. As Zaitahas pointed out, because you don't know how many there are, you mark the end of the list with a zero.
bnbertha/Zaita,

Thanks for the help!!!
Topic archived. No new replies allowed.