Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

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));
}
}
Avatar of Axter
Axter
Flag of United States of America image

A function that takes a pointer to pointer, usually does so because it wants to change what the caller's pointer is pointing to.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial