By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

struct hostent C

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

Expand|Select|Wrap|Line Numbers
  1. struct hostent {
  2. char*    h_name;       
  3. char**   h_aliases;    
  4.  int      h_addrtype;   ;; host address type 
  5. int      h_length;     ;; length of address 
  6. char**   h_addr_list;  ;; list of addresses 
  7. }
  8.  
is part of the C Library. Of this, I am interested in knowing
char** h_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 a lot.
Jul 10 '08 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
Of this, I am interested in knowing
char** h_aliases;points to an array of strings and uses NULL (or 0) as the last element in the list.
No it is not an array of strings, it is an array of pointers (which point to strings). A pointer can be NULL (that is not point to anywhere).

The actual value of the NULL pointer is platform dependent however the C/C++ standards state that the value 0 used in a pointer context is converted to the NULL pointer value for the target platform.

So you are not assigning an integer to a string, you are assigning an integer to a pointer and when you use 0, which the compiler knows is a place holder for the NULL pointer, it is valid but if you use any other integer value, say -1, the compiler does not know how to convert this to a pointer and you get a warning.


P.S. struct hostent is not part of the C library, it is part of the socket library.
Jul 10 '08 #2

P: 5
No it is not an array of strings, it is an array of pointers (which point to strings). A pointer can be NULL (that is not point to anywhere).

The actual value of the NULL pointer is platform dependent however the C/C++ standards state that the value 0 used in a pointer context is converted to the NULL pointer value for the target platform.

So you are not assigning an integer to a string, you are assigning an integer to a pointer and when you use 0, which the compiler knows is a place holder for the NULL pointer, it is valid but if you use any other integer value, say -1, the compiler does not know how to convert this to a pointer and you get a warning.


P.S. struct hostent is not part of the C library, it is part of the socket library.
Thanks, but why is the last argument an integer and not a string?
Jul 10 '08 #3

Expert 10K+
P: 11,448
Thanks, but why is the last argument an integer and not a string?
It's just a matter of definition; this is what the C99 Standard has to say about it:


[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant.48) If a null pointer constant is |
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.

[#4] Conversion of a null pointer to another pointer type
yields a null pointer of that type. Any two null pointers
shall compare equal.

[#5] An integer may be converted to any pointer type. |
Except as previously specified, the result is
implementation-defined, might not be properly aligned, and
might not point to an entity of the referenced type.49)
kind regards,

Jos
Jul 10 '08 #4

P: 5
It's just a matter of definition; this is what the C99 Standard has to say about it:



kind regards,

Jos
So, can we say that char** h_aliases is an integer pointer and which is why we use 0 as the last element denoting a null pointer?
Jul 10 '08 #5

Banfa
Expert Mod 5K+
P: 8,916
So, can we say that char** h_aliases is an integer pointer and which is why we use 0 as the last element denoting a null pointer?
No, char **h_aliases is a pointer to a pointer to a char. Since it is a pointer you can use 0 as the last element denoting a null pointer. integers have nothing to do with it.
Jul 10 '08 #6

P: 5
No, char **h_aliases is a pointer to a pointer to a char. Since it is a pointer you can use 0 as the last element denoting a null pointer. integers have nothing to do with it.
Thank you very much for clarifying.
Jul 10 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.