Pointer question

Posted on 2006-05-24
Last Modified: 2010-04-01
Hi folks,

I am confused by pointer, pointer to pointer. I do not understand code below (why  binTreeNode ** root?)

Could you pls give me your most personal & favotite view on pointer, pointer to pointer? Thanks. Pls based on code below. However, comparable & similar codes, if applicable, using pointer, pointer to do great job is appreciated. Many thanks.

Have a nice day!

Inserting a New Node in the Tree

void insert(int newKey, binTreeNode ** root)
// Pre: root points to the root of the tree or NULL
// Post: insert a new node in the subtree of lesser height
int leftHeight, rightHeight;
if (*root == NULL)
*root = createNode(newKey, NULL, NULL);
leftHeight = height((*root)->leftChild);
rightHeight = height((*root)->rightChild);
if (leftHeight < rightHeight)
insert(newKey, &((*root)->leftChild));
insert(newKey, &((*root)->rightChild));
Question by:tiger0516
    LVL 30

    Expert Comment

    A function that takes a pointer to pointer, usually does so because it wants to change what the caller's pointer is pointing to.
    LVL 30

    Accepted Solution

    Here's some example code:

    #include <stdlib.h>
    #include <iostream>
    using namespace std;

    char testsrc1[] =  "Hello World";
    char testsrc2[] =  "Good by World";

    void JustAPointer(char *data)
          data = testsrc2;

    void ApointerToAPointer(char **data)
          *data = testsrc2;

    void AReferenceToAPointer(char *&data)
          data = testsrc2;

    int main(int argc, char* argv[])
          char *OriginalPointer = testsrc1;
          cout << "OriginalPointer = " << OriginalPointer << endl;

          cout << "after JustAPointer, OriginalPointer = " << OriginalPointer << endl;

          cout << "after ApointerToAPointer, OriginalPointer = " << OriginalPointer << endl;
          OriginalPointer = testsrc1; //Reset value

          cout << "after AReferenceToAPointer, OriginalPointer = " << OriginalPointer << endl;

          return 0;

    LVL 30

    Expert Comment

    A function that only has a pointer, can not change the calling function's pointer.
    It can change the content of the pointe, but it can not change what it's pointing to.

    A function that either takes a pointer to a pointer or a reference of a pointer, can change what the calling function is pointing to.

    A pointer to a pointer can also be used for two dimensional arrays.
    See following links:
    LVL 5

    Assisted Solution

    See if you want to change anything in a function you have to pass the address (or a reference) of that variable....


    void fn1(int a)
      a = 10;

    and ur call is
    int num = 34;
    // After this call also the value of num is 34 only.... so if you want to change the value u have to pass the address....
    // So lets change the function a little....

    void fn2(int* a) // Pointer : address of a variable
      *a = 10;

    Now again make a call
    int num = 34;
    fn2(&num);  // Now passing the address of the variable....
    // So after the call the value of num will be 10... because we passed the address.....

    Ok now suppose you have a scenario where you have a pointer and you want to change the pointer itself....
    char *gStr = "Hello World";

    void fn3(char* msg) // A pointer
      msg = gStr;   // changing the pointer itself... not the data....

    So lets make a call....
    char *myStr = "Hey its my string";
    fn3(myStr); // Similar to fn1 passing value....
    //So after the call myStr will still point to "Hey its my string"......

    So you require an address to change anything... so if you want to change a pointer you have to pass the address of that pointer itself.... i.e. pointer to a pointer.....

    void fn4(char** msg) // A pointer
      *msg = gStr;   // changing the pointer itself... not the data....

    Now lets make the call.....
    char *myStr = "Hey its my string";
    fn4(&myStr); // Similar to fn2 passing the address....
    // After the call you will see the value has been changed.....

    I hope you got the concept....


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now