更改按值传递的指针 [英] change pointer passed by value
本文介绍了更改按值传递的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我给了一个函数foo(struct node *n)
,其中n
是链表中的头节点.
现在foo
应该更改n
s.t.它指向列表的末尾.
I have given a function foo(struct node *n)
where n
is the head node in a linked list.
Now foo
should change n
s.t. it points to the end of the list.
但是这个函数签名有可能吗?
But is this possible with this function signature?
假设t
是指向列表末尾的指针:
Assuming t
is the pointer to the end of the list:
-
n = t
将不起作用,因为指针是按值传递的. -
*n = *t
不起作用,因为我会覆盖列表的开头.
n = t
won't work because the pointer is passed by value.*n = *t
won't work because I would overwrite the head of the list.
我错过了什么吗?
推荐答案
您将需要使用指向指针的指针:
You would need to use a pointer to a pointer:
foo(struct node **n)
要更改n
指向的内容,请执行以下操作:
To change what n
points to, you do:
*n = t;
这篇关于更改按值传递的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文