gpt4 book ai didi

c - 通过点作为 C 中的引用?

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:04 24 4
gpt4 key购买 nike

在 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? */

}

[编辑]

我只想在这里澄清几件事,

我实际上有一个 void 函数,它接受一个 struct 参数,

void function (struct value arg){ }

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

node* list-> list;

所以在我的函数中,如果我做类似的事情,

arg->list

我访问的是原始列表变量吗?

最佳答案

回答您的编辑:

不,您是按值传递结构,因此函数包含结构的副本。如果您不返回函数中的结构并从调用函数的位置获取它,它将丢失。

请记住,C 不会通过引用传递。它是通过传递指针来模拟的。你可以改为这样做:

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

并且会修改原来的结构。

关于c - 通过点作为 C 中的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com