Public Group

#### Archived

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

# Changing a pointers address pointed to (in a function)

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

## 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;
};

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

{
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 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 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 on other sites
To eliminate some of the errors try using Node* &List as the post said and not Node&* List as you claimed to have used.

##### 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

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 30
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631741
• Total Posts
3001981
×