Thread: gethostbyname C function

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Smile gethostbyname C function

    Hi all,

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

    Code:
    struct hostent {
       138	        char*    h_name;       ;; official name
       139	        char**   h_aliases;    ;; alias list
       140	        int      h_addrtype;   ;; host address type 
       141	        int      h_length;     ;; length of address 
       142	        char**   h_addr_list;  ;; list of addresses 
       143	     };
    is part of the C Library. Of this, I am interested in knowing
    Code:
    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

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    you get a compiler warning .... why is the compiler willing to accept
    It isn't willing to accept it, so it gives a warning. How/what exactly are you initializing? (And why?) (code snip?) Usually you're reading a hostent, not writing to one...
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *hosten[] =
    		{ "test", "ing", "this", -1};
    }
    Code:
    $ gcc -o oddprog tomfoolery.c
    tomfoolery.c: In function `main':
    tomfoolery.c:6: warning: initialization makes pointer from integer without a cast
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Question

    Quote Originally Posted by Cactus_Hugger View Post
    It isn't willing to accept it, so it gives a warning. How/what exactly are you initializing? (And why?) (code snip?) Usually you're reading a hostent, not writing to one...
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *hosten[] =
    		{ "test", "ing", "this", -1};
    }
    Code:
    $ gcc -o oddprog tomfoolery.c
    tomfoolery.c: In function `main':
    tomfoolery.c:6: warning: initialization makes pointer from integer without a cast

    Lets say that we forget -1 for a moment, what I would like to understand is why would char** h_aliases accept an integer as the last argument and not a string?
    Last edited by ladesidude; 07-09-2008 at 09:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM