Thread: for loop question

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    for loop question

    The question is about looping through the following structure

    Code:
    struct  hostent { 
            char    *h_name;        /* official name of host */ 
            char    **h_aliases;    /* alias list */ 
            int     h_addrtype;     /* host address type */ 
            int     h_length;       /* length of address */ 
            char    **h_addr_list;  /* list of addresses from name server */ 
    #ifndef _POSIX_C_SOURCE 
    #define h_addr  h_addr_list[0]  /* address, for backward compatiblity */ 
    #endif /* !_POSIX_C_SOURCE */ 
    };
    ive seen people use
    Code:
    //with pointers
    for( char const*const* alias = hp->h_aliases; 
           *alias != NULL; 
           ++alias
    and

    Code:
    // with indexing 
      for( int i=0; 
           hp->h_aliases[i]; 
           ++i)
    When you use a for loop using pointers this case, why is the person explicit about NULL, but with indexing, it's implicit? ie not using

    hp->h_aliases[i] != 0

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdalten
    When you use a for loop using pointers this case, why is the person explicit about NULL, but with indexing, it's implicit?
    Dunno. Personal preference and/or laziness?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay. I thought I was maybe missing something.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Explicitly testing against NULL may be intended to make it clearer to those who may not see the equivalence as clearly.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I always do != NULL because it's a define so I pretend that it has an unknown value even though I'm aware it's 0 which I guess is good because then my code can be compiled in the year 4352 where NULL has been changed to (-243). Besides, all compilers will optimize if (var != 0) to become if (var).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM