C 中引用传递的微妙之处
我正在维护一些由最后担任我工作的人编写的 C 代码。出现了关于通过引用传递的情况。这是我正在使用的一个简短的、人为的示例:
static int b;
void SetToTen(int *a){
b = 10;
/* >>>>>>> Need to set a equal to b on this line <<<<<<<< */
return;
}
int main{
int a = 0;
SetToTen(&a);
/* Now a should be equal to 10*/
.
.
.
return 0;
}
在 SetToTen 函数中,我可以写:
*a = b;
或者
a = &b;
我认为这两个函数在功能上是等效的(其中任何一个 a 都等于 10。)但我的问题是:两者之间是否存在任何微妙的联系?具体来说,如果我使用 a = &b 是否意味着如果我将来更改 b,a 也会更改?如果我使用 *a = b 不是这样吗?
想法/沉思/评论将不胜感激。
I'm maintaining some C code that was written by the person who had my job last. A situation has come up regarding passing by reference. Here's a shortened, contrived example of what I'm working with:
static int b;
void SetToTen(int *a){
b = 10;
/* >>>>>>> Need to set a equal to b on this line <<<<<<<< */
return;
}
int main{
int a = 0;
SetToTen(&a);
/* Now a should be equal to 10*/
.
.
.
return 0;
}
In the SetToTen function, I could either write:
*a = b;
OR
a = &b;
I think these two are functionally equivalent (a will be equal to ten with either of them.) But my question is: are there any sneaky subtleties associated with one over the other? Specifically, if I use a = &b does that mean that if I change b in the future, a will change as well? And is this not the case if I use *a = b?
Thoughts/Musings/Comments would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们在功能上根本不等同。此代码:
取消引用 a 并将其值设置为 b 的值。此代码:
将 a 指针更改为指向 b。如果您使用第二段代码,则不会发生任何事情,因为您正在按值更改a指针,它实际上根本不会更改a。
They're not functionally equivalent at all. This code:
dereferences a and sets its value to that of b. This code:
changes the a pointer to point to b. If you use the second piece of code, nothing will happen because you're changing the a pointer by value, it won't actually change a at all.
这是正确的。
这只是将地址复制到值中。 C 语言按值传递所有 参数。 (C++有引用的概念)如果要修改传入函数的参数,需要传入地址或数组,并修改地址或数组成员引用的值。
最好按如下方式声明和定义该函数:
通过您的这些更改:
pa
(指向 a 的指针)避免了使用a
作为值的混乱你的 main 函数和a
作为 SetToTen() 中的指针类比:有两栋房子:史密斯先生的房子位于主街 115 号,蓝色,琼斯先生的房子位于埃尔姆街 205 号,绿色。你有一张纸条,上面有史密斯先生的地址。
*a = b;
就像将纸条上地址处的房子涂成与琼斯先生的房子相同的颜色。a = &b;
就像把纸条上的地址改为琼斯先生的地址;它实际上并没有做任何重要的事情。That's the correct one.
That's just copying an address into a value. The C language passes all arguments by value. (C++ has the notion of references) If you want to modify an argument passed into a function, you need to pass in an address or an array, and modify the value the address or array member refers to.
It might be better to declare and define the function as follows:
with these changes from yours:
pa
(pointer-to-a) avoids confusion of usinga
as a value in your main function anda
as a pointer in SetToTen()return
statement is not necessary unless you are trying to return a value (your function is declared void) or if you wish to return prematurely before the end of the function.Analogy: There are two houses: Mr. Smith's at 115 Main St., which is blue, and Mr. Jones at 205 Elm St., which is green. You have a slip of paper containing Mr. Smith's address.
*a = b;
is like painting the house at the address on the slip of paper the same color as Mr. Jones's house.a = &b;
is like changing the address on the slip of paper to be Mr. Jones's address; it doesn't actually do anything of consequence.