Thread: Double pointer question

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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 10:01 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    28,413
    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 10:08 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01:53 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06: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