• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Pointer question

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);
else
{
leftHeight = height((*root)->leftChild);
rightHeight = height((*root)->rightChild);
if (leftHeight < rightHeight)
insert(newKey, &((*root)->leftChild));
else
insert(newKey, &((*root)->rightChild));
}
}
0
tiger0516
Asked:
tiger0516
  • 3
2 Solutions
 
AxterCommented:
A function that takes a pointer to pointer, usually does so because it wants to change what the caller's pointer is pointing to.
0
 
AxterCommented:
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;

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

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

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


      system("pause");
      return 0;
}

0
 
AxterCommented:
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:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
0
 
dennis_georgeCommented:
See if you want to change anything in a function you have to pass the address (or a reference) of that variable....

e.g.

void fn1(int a)
{
  a = 10;
}

and ur call is
int num = 34;
fn1(num);
// 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....
Cheers,
Dennis


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now