I thought I understood pointers.

  • Thread starter noagbodjivictor
  • Start date
N

noagbodjivictor

Hi all,

I thought I knew how pointers work, but then I'm struggling with a
code, well I happen to have written last semester in my data structure
class. Why do we pass the pointer to the node of a binary tree by
reference? Isn't a pointer supposed to hold the address of an object
in memory?

Take this for example:
ItemType findMax(TreeNode* & tree) {
while(tree->right != NULL)
tree = tree->right;
return tree->info;
}

Why are we are passing the pointer tree by reference here? Don't be
too harsh, I just happen to have forgotten a little bit...
 
K

Kai-Uwe Bux

Hi all,

I thought I knew how pointers work, but then I'm struggling with a
code, well I happen to have written last semester in my data structure
class. Why do we pass the pointer to the node of a binary tree by
reference? Isn't a pointer supposed to hold the address of an object
in memory?

Take this for example:
ItemType findMax(TreeNode* & tree) {
while(tree->right != NULL)
tree = tree->right;
return tree->info;
}

Why are we are passing the pointer tree by reference here? Don't be
too harsh, I just happen to have forgotten a little bit...

The code seems to search for the right-most leaf in a tree. What passing the
parameter tree by reference does for you is that the function changes the
variable so that it will point to the right-most leaf after the call.


Best

Kai-Uwe Bux
 
E

Eric Pruneau

Hi all,

I thought I knew how pointers work, but then I'm struggling with a
code, well I happen to have written last semester in my data structure
class. Why do we pass the pointer to the node of a binary tree by
reference? Isn't a pointer supposed to hold the address of an object
in memory?

Take this for example:
ItemType findMax(TreeNode* & tree) {
while(tree->right != NULL)
tree = tree->right;
return tree->info;
}

Why are we are passing the pointer tree by reference here? Don't be
too harsh, I just happen to have forgotten a little bit...

Consider the following functions

void Changed(int* &val) { val = NULL; }

void UnChanged(int * val) { val = NULL; }

then here is what happenning when you use them.

int main()
{
int *value = new int;
*value = 54;

UnChanged(value); // value is NOT NULL after this call. value[0] is
still 54
Changed(value); // now value is NULL after this call
}

The difference is Changed accept a reference to a pointer. For example, if
the address of value in main() is 0x004839b0, then the address of val in
Changed() will be the same. So if you change val in Changed(), you end up by
changing value too.

The adress of val in UnChanged will NOT be the same as the address of value.
So changing the address of the pointer value of UnChanged do not affect the
address of the pointer value of main().

But if you modify Changed and UnChanged like that:

void Changed(int* &val) { val[0] = 0; }

void UnChanged(int * val) { val[0] = 0; }

Now both function change the value of main's value[0]. there is a
difference between changing the pointer address and changing the value it
point to.

in your findMax function, you have the line

tree = tree->right;

so you want to change the address of the pointer tree here, not only the
value if point to. So you need the & in the function declaration.

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top