for loop again. I don't understand the construction.

This is a discussion on for loop again. I don't understand the construction. within the C Programming forums, part of the General Programming Boards category; I posted a question about this maybe a two days ago. I realized I don't understand the constructions. Let me ...

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

    for loop again. I don't understand the construction.

    I posted a question about this maybe a two days ago. I realized I don't understand the constructions. Let me rattle on.

    Given the 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 */ 
    };
    h_aliases can get retrieved using either

    Code:
    //with pointers
    for( char const*const* alias = hp->h_aliases; 
           *alias != NULL; 
           ++alias)
    or

    Code:
    // with indexing 
      for( int i=0; 
           hp->h_aliases[i]; 
           ++i)
    I just realized I don't understand the pointer constructions for either

    // with indexing
    hp->h_aliases[i];

    or
    char const*const* alias = hp->h_aliases;

    That is, I don't know how they derived this. Can someone break this down into laymans terms or at least point in the right direction on how they got this?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Hmm.... Dave, you there? I'm not seeing this. I suppose if all else fails, I'll clean up the wording and post the same question comp.unix.programmer. I'm trying to avoid that route because it takes me like 10 days to make heads or tails out of some of the responses I get.
    Last edited by cdalten; 03-22-2006 at 08:02 AM.

  3. #3
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    cdalten.....

    First, keep in mind that I haven't had a second cup of coffee yet, and I usually need that to be anything remotely resembling sharp. Second, I'm performing a massive brain context switch here at the moment, but this beats working on the problem I'm stuck on.....

    Code:
    char **h_aliases
    Is a pointer to an array of strings. It can also be expressed as:

    Code:
    char *h_aliases[ ]
    That means that you can find the members of that array as either:

    Code:
    hp->h_aliases[0]   /* first array member */
    hp->h_aliases[1]  /* second array member */
    
    /* or */
    /* create a pointer to the first member.... */
    
    char const*const* alias = hp->h_aliases;  
    
    /* and traverse the array by incrementing the pointer */
    
    ++alias;
    When trying to wrap your brain around this, you can ignore (For now) the const keywords -- he's just saying that he's not going to modify the pointer or the contents....

    Hope I shed a little light on this.... I'm going for coffee now
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The coffee's ready?

    cdalten, it may be easier to think about this using the parameterized declaration of main; argv is very similar in usage to h_aliases. At least I'm hoping that it somehow can help clear the fog this way.
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
       char **arg;
       int i;
    
       for ( i = 0; argv[i] != NULL; ++i )
       {
          puts(argv[i]);
       }
    
       for ( arg = argv; *arg != NULL; ++arg )
       {
          puts(*arg);
       }
       return 0;
    }
    
    /* my output
    H:>test one two three
    H:\test.exe
    one
    two
    three
    H:\test.exe
    one
    two
    three
    */
    Last edited by Dave_Sinkula; 03-22-2006 at 09:02 AM. Reason: Trimmed unnecessary leftover header in the code.
    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
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    One of these times I'm going to sit down and think the problem through before I just hit the panic button and post the question.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay. I was also mystified how

    Code:
    hp->h_aliases[i]
    Breaks down.

    Isn't it something like:

    (*hp).h_aliases[i] --> (*hp).*(h_aliases+i)

    Okay, I'll take this up later. I need to get gas in my car and drag my butt into work.

    Thanks for the help

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Dave_Sinkula
    The coffee's ready?

    We're lucky here. We have one of those one-cup coffee makers with the little K-cups. It can be ready any time I drag my butt down to the break room

    Thanks for clarifying the explanation. I never thought about telling cdalten about argv.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. construction order
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 04-30-2008, 09:16 AM
  2. Replies: 29
    Last Post: 12-13-2004, 05:20 PM
  3. construction and induction proofs help
    By axon in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-09-2004, 10:02 AM
  4. static members construction
    By glUser3f in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2003, 09:58 PM
  5. OO Compiler construction
    By daljian in forum C++ Programming
    Replies: 0
    Last Post: 01-04-2002, 08:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21