Archived

This topic is now archived and is closed to further replies.

Changing a pointers address pointed to (in a function)

This topic is 5339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an AP test on this stuff tommorow, and while reviewing, I came across something baffling. Consider the following code.
    
#include <iostream>

using std::cout;
using std::endl;

struct Node
{
	Node *next;
	int data;
};

void ChangeAddress(Node *List);

int main()
{
	Node * List  = new Node;
	cout << "List Address: " << List << endl;
	ChangeAddr(List);
	cout << "List Address Now: " << List << endl;
	return 0;
}

void ChangeAddress(Node *List)
{
	List = NULL;
	cout << "List Address set to NULL: " << List << endl;
	return;
}
  
My question is this: how would I write a function that can change where the pointer points to. If this were anything other than a pointer, I would simply pass by reference. My study book hinted that the way I would get around this problem was to pass a pointer by reference, but I've never seen or heard of pointers being passed by reference , please correct me if I am horribly wrong. As for now, I'm stumped. :[ [edited by - LuckyNewbie on May 6, 2003 10:45:42 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by LuckyNewbie
I have an AP test on this stuff tommorow, and while reviewing, I came across something baffling. Consider the following code.


      
#include <iostream>

using std::cout;
using std::endl;

struct Node
{
Node *next;
int data;
};

void ChangeAddress(Node *List);

int main()
{
Node * List = new Node;
cout << "List Address: " << List << endl;
ChangeAddr(List);
cout << "List Address Now: " << List << endl;
return 0;
}

void ChangeAddress(Node *List)
{
List = NULL;
cout << "List Address set to NULL: " << List << endl;
return;
}


My question is this: how would I write a function that can change where the pointer points to. If this were anything other than a pointer, I would simply pass by reference. My study book hinted that the way I would get around this problem was to pass a pointer by reference, but I''ve never seen or heard of pointers being passed by reference , please correct me if I am horribly wrong. As for now, I''m stumped. :[

[edited by - LuckyNewbie on May 6, 2003 10:45:42 AM]

Yes, either pass the pointer by reference - easiest way to do it, or make the paramater a pointer to a pointer but you''d have to change the function call as well.

So to pass by reference, simply change the parameter to Node*& List, and leave the function call as is.

Or, you can have the parameter point to a pointer, hence changing it to Node** List, however you''d have to change the call so that it passes &List as the argument.

Hope it helps.

Share this post


Link to post
Share on other sites
quote:
Original post by Arkainium
So to pass by reference, simply change the parameter to Node*& List, and leave the function call as is.



Thanks a bunch, I only tried Node&* List and got some errors. Exactly what I needed to know :-]

Share this post


Link to post
Share on other sites
quote:
Original post by Code-Junkie
To eliminate some of the errors try using Node* &List as the post said and not Node&* List as you claimed to have used.


I was using "Node& *List" before I asked, getting errors. Now I use "Node* &List" w/ no errors. Guess I should have cleared that up the first time

Share this post


Link to post
Share on other sites