C 中引用传递的微妙之处

发布于 2024-12-20 04:48:56 字数 639 浏览 1 评论 0原文

我正在维护一些由最后担任我工作的人编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-12-27 04:48:56

它们在功能上根本不等同。此代码:

*a = b;

取消引用 a 并将其值设置为 b 的值。此代码:

a = &b;

a 指针更改为指向 b。如果您使用第二段代码,则不会发生任何事情,因为您正在按值更改a指针,它实际上根本不会更改a

They're not functionally equivalent at all. This code:

*a = b;

dereferences a and sets its value to that of b. This code:

a = &b;

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.

堇年纸鸢 2024-12-27 04:48:56
*a = b;

这是正确的。

a = &b;

这只是将地址复制到值中。 C 语言按值传递所有 参数。 (C++有引用的概念)如果要修改传入函数的参数,需要传入地址或数组,并修改地址或数组成员引用的值。

最好按如下方式声明和定义该函数:

void setToTen(int *pa){
    *pa = 10;
}

通过您的这些更改:

  1. pa(指向 a 的指针)避免了使用 a 作为值的混乱你的 main 函数和 a 作为 SetToTen() 中的指针
  2. 如果目标是将传入地址的变量设置为 10,则没有真正的理由在任何地方使用 b。
  3. 风格/语法:大多数 C编码约定使用以以下开头的符号小写字母作为变量/方法,以大写字母开头的符号作为类型。
  4. 风格/语法:除非您尝试返回一个值(您的函数被声明为 void)或者您希望在函数结束之前提前返回,否则不需要显式的 return 语句。

类比:有两栋房子:史密斯先生的房子位于主街 115 号,蓝色,琼斯先生的房子位于埃尔姆街 205 号,绿色。你有一张纸条,上面有史密斯先生的地址。

*a = b; 就像将纸条上地址处的房子涂成与琼斯先生的房子相同的颜色。

a = &b; 就像把纸条上的地址改为琼斯先生的地址;它实际上并没有做任何重要的事情。

*a = b;

That's the correct one.

a = &b;

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:

void setToTen(int *pa){
    *pa = 10;
}

with these changes from yours:

  1. pa (pointer-to-a) avoids confusion of using a as a value in your main function and a as a pointer in SetToTen()
  2. There's no real reason to use b anywhere, if the goal is to set the variable whose address is passed in to 10.
  3. Style/syntax: most C coding conventions use symbols starting with lowercase as variables/methods, and symbols starting with uppercase as types.
  4. Style/syntax: an explicit 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文