开发者

C++: Passing pointer variable to function

开发者 https://www.devze.com 2023-03-20 10:27 出处:网络
I have a class Node: class Node { public: int item; Node * nextLink; }; Outside a function I declare a Node pointer:

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 开发者_JS百科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


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

try

void foo(Node*& node) 


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 );


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.)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号