Thread: Question About Pointer To Pointer

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Question About Pointer To Pointer

    My question is about pointers to pointers. A book I’m using to teach myself C++ uses a function whose first argument is a pointer to a pointer and receives the address of the root pointer into that binary tree class insertion function. It says that it uses the pointer to a pointer in order to modify the pointer's value. In general, why would one ever have to use a pointer to a pointer, instead of just passing the pointer itself? It just seems like a needless extra step to me.

    The code is posted below:

    Code:
    template< class NODETYPE>
    void Tree<NODETYPE>::insertNode(const NODETYPE &value)
    {
    	insertNodeHelper( &rootPtr, value);
    }
    
    template< class NODETYPE>
    void Tree<NODETYPE>::findNode(const NODETYPE &value)
    {
    	findNodeHelper( &rootPtr, value);
    }
    
    template< class NODETYPE>
    void Tree<NODETYPE>::insertNodeHelper( TreeNode< NODETYPE> **ptr, const NODETYPE &value)
    {
    	if(*ptr == 0)
    		*ptr = new TreeNode< NODETYPE> (value);
    
    	else
    		if (value < (*ptr)->data)
    			insertNodeHelper(&((*ptr)->leftPtr), value);
    		else
    			if (value >(*ptr)->data)
    				insertNodeHelper(&((*ptr)->rightPtr), value);
    			else cout << value << " dup" << endl;
    }

  2. #2
    I'm quite sure a seach on these boards alone will reveal this question having been answered in far greater detail, but suffice to say that it is indeed very very useful. Indeed, finding a pointer to pointer to pointer is entirely commonplace in some areas of my code.

    Possible reasons:

    Dynamicly allocated Matrices;
    Passing a pointer to a function which will then set the pointer to the correct location. (Though this could also be done using a reference to a pointer)

    You will also find a lot of p to p instances in DirectX, such as the Lock method of LPDIRECT3DVERTEXBUFFER9 just off the top of my head.

    /*Edit*/

    And looking at the posted code; the reasons are the calls to insertNodeHelper:

    insertNodeHelper(&((*ptr)->leftPtr), value);

    to learn, try passing in just the pointer, remove the dereference from the insertNodeHelper lines and watch the app fail.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> It says that it uses the pointer to a pointer in order to modify the pointer's value.
    Perhaps this will help you wrap your head around it...
    Code:
    #include <iostream>
    using namespace std;
    
    void foo(void *p)
    {
        p = (void*)3;
    }//foo
    
    void bar(void **p)
    {
        *p = (void*)3;
    }//bar
    
    void cpp(void *&p)
    {
        p = (void*)5;
    }//cpp
    
    int main()
    {
        void *p = 0;
    
        foo(p);
        cout << "p is " << int(p) << " after calling foo(p)" << endl;
    
        bar(&p);
        cout << "p is " << int(p) << " after calling bar(p)" << endl;
    
        cpp(p);
        cout << "p is " << int(p) << " after calling cpp(p)" << endl;
    
        return 0;
    }//main
    Why the author didn't make ptr a reference parameter is beyond me - probably yet another C programmer who wrote a C++ book.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM