Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. C++ Pointer To Pointer

C++ Pointer To Pointer

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++data-structureshelplounge
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlitzPackage
    wrote on last edited by
    #1

    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: template< class NODETYPE> void Tree::insertNode(const NODETYPE &value) { insertNodeHelper( &rootPtr, value); } template< class NODETYPE> void Tree::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; } Any help or insight would be greatly appreciated.

    P V J 3 Replies Last reply
    0
    • B BlitzPackage

      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: template< class NODETYPE> void Tree::insertNode(const NODETYPE &value) { insertNodeHelper( &rootPtr, value); } template< class NODETYPE> void Tree::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; } Any help or insight would be greatly appreciated.

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      passing pointer to any function passes copy of that pointer. It will give problem when you change the variable it is addressing. Good article is present on CP,which has explained with example the problem. http://www.codeproject.com/cpp/PtrToPtr.asp#1[^]

      1 Reply Last reply
      0
      • B BlitzPackage

        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: template< class NODETYPE> void Tree::insertNode(const NODETYPE &value) { insertNodeHelper( &rootPtr, value); } template< class NODETYPE> void Tree::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; } Any help or insight would be greatly appreciated.

        V Offline
        V Offline
        VaporTrace
        wrote on last edited by
        #3

        There are also a lot of techniques in which you may pass processing to one of a set of functions by means of a switch statement. If you have an array of function pointers you could simply pass to that offset in your array of pointers. There's also ease of adding a new function to that list where all you need to do is add an extra switch statement and the pointer into the array.

        1 Reply Last reply
        0
        • B BlitzPackage

          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: template< class NODETYPE> void Tree::insertNode(const NODETYPE &value) { insertNodeHelper( &rootPtr, value); } template< class NODETYPE> void Tree::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; } Any help or insight would be greatly appreciated.

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          Normaly you try to write your code so that passing a pointer to a pointer is not needed, but some times it is needed.

          // A bad example
          int alloc_type(NODE** ptr)
          { return( (*ptr=(NODE*)malloc(sizeof(NODE)) != (NODE*)0) ); }
          //
          somefunc()
          {
          NODE* ptr = (NODE*)0;
          if( alloc_type(&ptr) )
          // do some thing with the allocated data
          }

          What the provided code is doing: 1) The first time insertNodeHelper is called, rootPtr is null, so it just allocates a new node. That is, rootPtr now points to a node. 2) The second time insertNodeHelper is called it recursively calls itself to allocate/add a new leaf node. The recursive call has no effect on the rootPtr. 3) See (2). The code you provided is a good example of when you might use a pointer to a pointer as an argument. It is very creative and provides a simple solution for what could be a difficult problem. I do not know if a latter example shows you how to remove the recursive call, but there should be one. If not, consider it your next assignment.:laugh: INTP Every thing is relative...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups