Pointers on pointers to pointers please...

This is a discussion on Pointers on pointers to pointers please... within the C Programming forums, part of the General Programming Boards category; I have written a program which uses lists of structures. I am attempting to re-write with the functions broken down ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    98

    Pointers on pointers to pointers please...

    I have written a program which uses lists of structures. I am attempting to re-write with the functions broken down further, and am getting confused with pointers.

    Here is some code, the basic purpose of which is to append a node onto a list. It should be passed the address of the current ultimate node in the list, and return the address of the new node once it's been appended. This new node would then be passed as the new ultimate node in the next call to the function.

    Code:
    struct node{
      char string[8];
      int    indicator;
      struct node *next_node;
      };
    
    calling_func()
    {
      struct node *head_node = NULL;
      struct node *new_node = NULL;
    
      ...
    
      new_node = append( *head_node, "string" );
    
      ...
    }
    
    struct node *
    append( struct node **node,
               char *word )
    {
      struct node *new_node = NULL;
    
      new_node = (struct node *) malloc( sizeof( struct node ));
      new_node->next_node = NULL;
    
      new_node->indicator = 0;
      strcpy( new_node->string, word );
    
      if( *node == NULL )
        *node = new_node;
      else
        *node->next_node = new_node; /*Compiler errors this line*/
    
      return( &new_node );
    }
    I get two errors when compiling:
    1. warning: left operand of "->" must be pointer to struct/union

    2.assignment type mismatch:
    struct node {array[8] of char string, int indicator, pointer to struct node {..} next_node} "="
    pointer to struct node {array[8] of char string,
    int indicator, pointer to struct node {..} next_node}

    I don't understand why the compiler accepts the *node = new_node statement, but not the *node->next_node = new_node statement.
    I've got confused in the if/else block of the append function, and I can't figure out what *node, or **node, or node or &node represent when they are in this function.

    Could someone please explain?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You seem to be having trouble with the concept of a pointer to a pointer:

    >new_node = append( *head_node, "string" );

    head_node is declared as a pointer to a struct, as such, when you dereference it you get a struct. This is a type mismatch, to pass a pointer to head_node, you do this:

    new_node = append( &head_node, "string" );

    >*node->next_node = new_node; /*Compiler errors this line*/

    To get to the pointed to pointer, you must first dereference the top level, then you can have arrow access:

    (*node)->next_node = new_node;

    >return( &new_node );

    new_node is declared as a pointer to a struct, returning it's address means you're returning a pointer to a pointer. The function is declared as returning a pointer to a struct, what you want is

    return( new_node );
    Code:
    struct node *
    append( struct node **node, char *word )
    {
        struct node *new_node = NULL;
    
        new_node = (struct node *) malloc( sizeof( struct node ));
        new_node->next_node = NULL;
    
        new_node->indicator = 0;
        strcpy( new_node->string, word );
    
        if( *node == NULL )
            *node = new_node;
        else
            (*node)->next_node = new_node;
    
        return( new_node );
    }
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Ah yes thankyou...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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