change pointer passed by value

怎甘沉沦 提交于 2019-11-27 09:21:16

You would need to use a pointer to a pointer:

foo(struct node **n) 

To change what n points to, you do:

*n = t;

No, because you have a copy of the value of the pointer that's been passed in. You have no access to the original pointer that was passed in. In order to modify the pointer outside the function the sig would need to be foo( struct node **n).

You didn't miss anything. It's not possible with the given function declaration.

As you wrote, the pointer is passed by value, therefore if changed, it won't propagate to the original variable.

You need to change the prototype to foo(struct node **n); or struct node *foo(struct node *n); and return the new pointer as result.

No, you cannot change any argument passed by value.

You can't change n and have the caller see the change. You can change n->prev->next and n->next->prev though - that may be what you need.

It is impossible for the change to be reflected in the calling function. However, inside foo, you can change n to your heart's content.

int foo(struct node *n) {
    n = NULL; /* ok */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!