我的传引用代码在C中为什么不起作用?

huangapple go评论78阅读模式
英文:

Why is my pass by reference code not working in C?

问题

以下是代码的翻译部分:

  1. #include <stdio.h>;
  2. void set_array(int array[4]);
  3. void set_int(int x);
  4. int main(void) {
  5. int a = 10;
  6. int b[4] = { 0, 1, 2, 3 };
  7. set_int(a);
  8. set_array(b);
  9. printf("%d %d\n", a, b[0]);
  10. }
  11. void set_array(int array[4]) {
  12. array[0] = 22;
  13. }
  14. void set_int(int x) {
  15. x = 22;
  16. }
英文:

Can anyone point out why I am getting an error in this code and what should be the fix? This did seem to work for others. Thank you!

I am trying to code an example for pass by reference for arrays

  1. #include &lt;stdio.h&gt;
  2. void set_array(int array[4]);
  3. void set_int(int x);
  4. int main(void) {
  5. int a = 10;
  6. int b[4] = { 0, 1, 2, 3 };
  7. set_int(a);
  8. set_array(b);
  9. printf(&quot;%d %d\n&quot;, a, b[0]);
  10. }
  11. void set_array(int array[4]) {
  12. array[0] = 22;
  13. }
  14. void set_int(int x) {
  15. x = 22;
  16. }

答案1

得分: 4

以下是翻译好的部分:

  1. x = 22;
  2. }
  3. Why this is not working?
  1. 你提供的代码没有按预期工作,因为你在函数 `set_array` `set_int` 中传递参数时是按值传递,而不是按引用传递。在C语言中,函数参数是按值传递的,这意味着会创建参数的副本,对参数在函数内部的修改不会影响原始变量。
  2. `set_array` 函数中,当你修改 `array[0]` 时,会影响原始数组,因为在C中,数组是按引用传递的(数组名充当指向第一个元素的指针)。然而,在 `set_int` 函数中,你修改的是局部变量 `x`,它是 `main` 函数中 `a` 变量的副本。因此,修改不会影响原始的 `a` 变量。
  3. 要解决这个问题,你可以修改函数,接受指向变量的指针。以下是你的代码的更新版本,演示了指针的正确用法:
  4. #include <stdio.h>
  5. void set_array(int* array);
  6. void set_int(int* x);
  7. int main(void)
  8. {
  9. int a = 10;
  10. int b[4] = { 0, 1, 2, 3 };
  11. set_int(&a);
  12. set_array(b);
  13. printf("%d %d\n", a, b[0]);
  14. }
  15. void set_array(int* array)
  16. {
  17. array[0] = 22;
  18. }
  19. void set_int(int* x)
  20. {
  21. *x = 22;
  22. }
英文:
  1. x = 22;
  2. }

Why this is not working?

The code you provided is not working as intended because you are passing the arguments to the functions set_array and set_int by value instead of by reference. In C, function arguments are passed by value, which means that a copy of the argument is made and any modifications made to the argument within the function will not affect the original variable.

In the set_array function, when you modify array[0], it will affect the original array because arrays in C are passed by reference (the array name acts as a pointer to the first element). However, in the set_int function, you are modifying the local variable x, which is a copy of the a variable in the main function. Therefore, the modification does not affect the original a variable.

To fix this issue, you can modify the functions to accept pointers to the variables instead. Here's an updated version of your code that demonstrates the correct usage of pointers:

  1. #include &lt;stdio.h&gt;
  2. void set_array(int* array);
  3. void set_int(int* x);
  4. int main(void)
  5. {
  6. int a = 10;
  7. int b[4] = { 0, 1, 2, 3 };
  8. set_int(&amp;a);
  9. set_array(b);
  10. printf(&quot;%d %d\n&quot;, a, b[0]);
  11. }
  12. void set_array(int* array)
  13. {
  14. array[0] = 22;
  15. }
  16. void set_int(int* x)
  17. {
  18. *x = 22;
  19. }

答案2

得分: 1

当你将参数传递给函数时,实际上是传递了一个值的副本。所以当你这样做时:

  1. void set_int(int x) {
  2. x = 22;
  3. }
  4. int main(int argc, char** argv) {
  5. int a = 20;
  6. set_int(22);
  7. printf("%d\n", a);
  8. }

你实际上是将变量a的值复制到了x,现在你有两个变量,a和x。x被设置为22,然后你从函数中返回,C会自动从内存中删除x。

为了获取正确的变量,你可以使用指针,指针是一个整数(在技术上是无符号长整数,但在我们的目的上是int),它表示内存中存储另一个值的位置。

  1. int main(int arc, char** argv) {
  2. int a = 20;
  3. int* b = &a;
  4. }

在这个示例中,b的类型是“int指针”,它指向a。我们可以更新set_int()函数来“解引用”我们的新指针值。

  1. void set_int(int* x) {
  2. // 使用*操作符解引用我们的int*,y是内存中与a相同位置的原始整数
  3. int y = *x;
  4. // 因为我们正在使用a,现在我们可以将它设置为22
  5. y = 22;
  6. }
  7. int main(int arc, char** argv) {
  8. int a = 20;
  9. int* b = &a;
  10. set_int(b);
  11. // 这应该打印出22
  12. printf("%d\n", a);
  13. return 0;
  14. }

请注意,set_int() 这样编写是为了帮助理解指针的工作原理,通常你可以将其压缩成一行:

  1. void set_int(int* x) {
  2. *x = 22;
  3. }

当然,我们也可以压缩我们的调用:

  1. int main(int argc, char** argv) {
  2. int a = 20;
  3. set_int(&a);
  4. printf("%d\n", a);
  5. return 0;
  6. }
英文:

When you pass an argument to a function you are actually passing a copy of the value. So when you do

  1. void set_int(int x) {
  2. x = 22;
  3. }
  4. int main(int argc, char** argv) {
  5. int a = 20;
  6. set_int(22);
  7. printf(&quot;%d\n&quot;, a);
  8. }

What you are essentially doing is copying the value of a and putting it into x, now you have to variables, a and x. x is set to 22 and you return out of the function, which C will remove x from memory automatically.

To get the correct variable you can use pointers which are an integer (technically unsigned long but int for our purposes) value that represent a point in memory where another value is stored.

  1. int main(int arc, char** argv) {
  2. int a = 20;
  3. int* b = &amp;a;
  4. }

In this example b is of type "int pointer" which points to a. We can update the set_int() function to "dereference" our new pointer value.

  1. void set_int(int* x) {
  2. // Dereference out int* with the * operator
  3. // y is out original integer a at the same spot in memory
  4. int y = *x;
  5. // Because we are using a we can now set it to out value of 22
  6. y = 22;
  7. }
  8. int main(int arc, char** argv) {
  9. int a = 20;
  10. int* b = &amp;a;
  11. set_int(b);
  12. // This should print 22
  13. printf(&quot;%d\n&quot;, a);
  14. return 0;
  15. }

Note that set_int() is written this way to help understand how pointers work, normally you would compact this into one line:

  1. void set_int(int* x) {
  2. *x = 22;
  3. }

And of course we can also compact our call:

  1. int main(int argc, char** argv) {
  2. int a = 20;
  3. set_int(&amp;a);
  4. printf(&quot;%d\n&quot;, a);
  5. return 0;
  6. }

huangapple
  • 本文由 发表于 2023年5月28日 10:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定