Erich Peterson
Erich Peterson

Reputation: 861

C++: Passing pointer variable to function

I have a class Node:

class Node {
public:
    int item;
    Node * nextLink;
};

Outside a function I declare a Node pointer:

Node * newNode;

Then, I pass this pointer to a function foo:

void foo(Node * node) {

  node = new Node();

  node->item = 1;

  Node * anotherNode = new Node();

  anotherNode->item = 2;

  node->nextLink = anotherNode; 
}

Actual call:

foo(newNode);

At the end of foo (but before existing) I can see that node->item and node->nextLink point to the correct places. However, when returned from foo, I check newNode, and the item is correct, but the nextLink is not there.

Any suggestions? Thanks

Upvotes: 2

Views: 4104

Answers (5)

Dave Costa
Dave Costa

Reputation: 48111

What you say is happening doesn't quite make sense to me. Since you are passing the pointer variable by value, its value in the calling function should not be changed at all, as far as I can see, so you actually shouldn't see any of the results of your function.

Although I can't explain the details of what's happening in your current code, I highly recommend you try it this way:

Node * foo(void) {

  Node * node = new Node();

  node->item = 1;

  Node * anotherNode = new Node();

  anotherNode->item = 2;

  node->nextLink = anotherNode;

  return node;
}

and call like:

Node *newNode = foo();

Since your function doesn't actually do anything with the pointer value that is passed in, it makes more sense for it to create everything using local variables and return pointer to the newly allocated node.

(You can also pass a reference to the pointer as other have suggested, but I don't see that as good programming style in this case. It is clearer to simply return the pointer from the function and assign it to the appropriate variable in the calling code.)

Upvotes: 3

phlogratos
phlogratos

Reputation: 13924

If you want the function foo to change the pointer, you have to call it with the adress of the pointer, like this:

Node * newNode;

void foo(Node ** nodepointer) {
    (*nodepointer) = new Node();
    (*nodepointer)->item = 1;
    Node * anotherNode = new Node();
    anotherNode->item = 2;
    anotherNode->nextLink = NULL;
    (*nodepointer)->nextLink = anotherNode; 
}

foo(&newNode);

As shown above you should also set anotherNode->nextLink to NULL to mark the end of the list.

Upvotes: 2

Kevin
Kevin

Reputation: 25269

You have three choices. You can pass a Node** as input, a Node*& as input, or you can return a Node* from the foo method. All three options work.

Upvotes: 2

Praetorian
Praetorian

Reputation: 109109

Within foo() you're modifying a local copy of the pointer being passed to the function. Change foo to the following:

void foo(Node ** node) {

  *node = new Node();

  (*node)->item = 1;

  Node * anotherNode = new Node();

  anotherNode->item = 2;

  (*node)->nextLink = anotherNode; 
}

Then the call becomes:

foo( &newNode );

Upvotes: 5

Josh
Josh

Reputation: 6473

You need to pass a reference. You are modifying a copy of the pointer

try

void foo(Node*& node) 

Upvotes: 5

Related Questions