Double pointer question

This is a discussion on Double pointer question within the C Programming forums, part of the General Programming Boards category; if i have a struct define in main for example Code: struct node { int key_value; struct node *left; struct ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Double pointer question

    if i have a struct define in main for example
    Code:
    struct node
    {
      int key_value;
      struct node *left;
      struct node *right;
    };
    
    insert(int key, struct node **leaf)
    {
        if( *leaf == 0 )
        {
            *leaf = (struct node*) malloc( sizeof( struct node ) );
            (*leaf)->key_value = key;
            /* initialize the children to null */
            (*leaf)->left->left = 0;    
            (*leaf)->left->right = 0;  
        }
        else if(key < (*leaf)->key_value)
        {
            insert( key, &(*leaf)->left );
        }
        else if(key > (*leaf)->key_value)
        {
            insert( key, &(*leaf)->right );
        }
    }
    Because i use the double pointer if i was to write a print or search function would i need to pass a double pointer? I know it works if i do but want to be sure is this the only way? Here is example of the rest of my code
    Code:
    void print(struct node **leaf)
    {
    	printf("here");
    	printf("are we here %d\n", (*leaf)->key_value);
    	if(leaf!=NULL)
    	{
    		
           	 	//printf("Value of leaf in print is %d", leaf->key_value);
            	//print(&(*leaf)->left);
            	//print(&(*leaf)->right);
            }
    }
    
    struct node *search(int key, struct node **leaf)
    {
      if( leaf != 0 )
      {
          if(key==(*leaf)->key_value)
          {
              return (*leaf);
          }
          else if(key<(*leaf)->key_value)
          {
              return search(key, (*leaf)->left);
          }
          else
          {
              return search(key, (*leaf)->right);
          }
      }
      else return 0;
    }

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Quote Originally Posted by newbie30
    Because i use the double pointer if i was to write a print or search function would i need to pass a double pointer?
    No. The reason for the pointer to a pointer parameter is so that the result of malloc could be reflected in the caller (i.e., if you just passed a pointer to a struct node, the result of malloc would just be assigned to the local struct node pointer rather than the struct node pointer from the caller). If you do not need such a call by reference parameter (which is the case for a print or a search function), then just passing a pointer to a struct node will do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    When i pass jsut the pointer to the function, i get rubbish printed out.


    What would be the print command:


    printf("%d", leaf->key_value);


    Is that right?


    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Show the relevant code of the print function, along with the relevant code that calls the function.

    EDIT:
    That said, if the caller code is correct, and your "print command" as given is an obviously modified version of what you posted earlier, I see no problem.
    Last edited by laserlight; 08-20-2009 at 11:01 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm also not convinced that the Insert function is completely kosher (the code
    Code:
            /* initialize the children to null */
            (*leaf)->left->left = 0;    
            (*leaf)->left->right = 0;
    looks pretty wrong from here).

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    here ya go:

    Code:
    void print(struct node *leaf)
    {
    	printf("here");
    	printf("are we here in print  %d\n", leaf->key_value);
    
    }
    
    
    int main()
    {
    struct node *root;
    	struct node *s;
    	insert(2,root);
    	print(root);
    }
    thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    tabstop is correct, but your main function also has two problems: it did not initialise root to be a null pointer and it passed root instead of the address of root to the insert function. You probably should be compiling with a higher warning level than you do at the moment.
    Last edited by laserlight; 08-20-2009 at 11:08 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double astrix pointer question..
    By transgalactic2 in forum C Programming
    Replies: 104
    Last Post: 01-19-2009, 05:02 AM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 02:53 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 07:59 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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