Thread: pointer to pointer as argument question

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    pointer to pointer as argument question

    If I have a function, such as a delete function for a binary search tree, and I want to change the root node, which is a pointer within main(), The only way I read I could do that would be to pass a pointer to that pointer.

    for instance....

    void someFunc(node **root, int key);

    I am a little confused in how this should be used. I keep getting "Request for member in something not structure or union" with the following.

    Code:
    void delete(b_tree **tree, t_node *node, int key)
    {
    //code in here
    
        if(target == tree->root)
               tree->root = tmp;     //i think it should be *tree->root
    
    //later on
          tree->size++;
    }
    Any help on getting these pointers set right would be appreciated.

    EDIT: I initially thought this would work....

    Code:
      if (target == *tree->root)
        *tree->root = tmp;
    
    //later
      *tree->size++;
    Last edited by Lateralus; 07-21-2005 at 08:00 AM.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Well, after taking a look around I solved the problem.

    In order to use tree->root I needed to use: (*tree)->root;

    Mods, you can delete this if you wish, or other can view it for reference.
    Last edited by Lateralus; 07-21-2005 at 08:25 AM.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  3. #3
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    consider operators precendce

  4. #4
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Yea, the nice thing about running into problems like this is that it is very unlikely that I won't consider precedence in the future.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I've found that it's easier to avoid this issue entirely by returning the modified pointer. That way you generally don't have to fool around with pointers to pointers.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  2. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  3. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  4. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 09:51 AM
  5. file pointer as an argument?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-12-2001, 09:31 PM