传递点作为C中的参考?

时间:2015-06-04 15:41:52

标签: c pointers

在C中,我想将指针传递给另一个函数,所以我编写了以下代码,

node* list // contains linked list of nodes

void function (node* list){

/*Whatever I do here, I want to make sure that I modify the original list, 
  not the local copy. +

  how do I pass the original pointer 'list' to this function so that I'm working  
  on the same variable? */

}

[编辑]

我想在这里澄清一些事情,

我实际上有一个带有struct参数的void函数,

void function (struct value arg){ }

在结构中,我将我的一个内部变量定义为

node* list-> list;

所以在我的功能中,如果我做了类似的事情,

arg->list
我是否正在访问原始列表变量?

1 个答案:

答案 0 :(得分:1)

回答你的编辑:

不,您是按值传递结构,因此该函数包含结构的副本。如果你没有return函数中的结构并从你调用函数的地方抓取它,它就会丢失。

请记住,C不会通过引用传递。它通过传递指针进行模拟。你可以这样做:

void function (struct value* arg)
{
....
}

它将修改原始结构。