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

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

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

问题

以下是代码的翻译部分:

#include <stdio.h>;

void set_array(int array[4]);
void set_int(int x);

int main(void) {
    int a = 10;
    int b[4] = { 0, 1, 2, 3 };
    set_int(a);
    set_array(b);
    printf("%d %d\n", a, b[0]);
}

void set_array(int array[4]) {
    array[0] = 22;
}

void set_int(int x) {
    x = 22;
}
英文:

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

#include &lt;stdio.h&gt;

void set_array(int array[4]);
void set_int(int x);

int main(void) {
    int a = 10;
    int b[4] = { 0, 1, 2, 3 };
    set_int(a);
    set_array(b);
    printf(&quot;%d %d\n&quot;, a, b[0]);
}
void set_array(int array[4]) {
    array[0] = 22;
}

void set_int(int x) {
    x = 22;
}

答案1

得分: 4

以下是翻译好的部分:

x = 22;
}
Why this is not working?
你提供的代码没有按预期工作,因为你在函数 `set_array` 和 `set_int` 中传递参数时是按值传递,而不是按引用传递。在C语言中,函数参数是按值传递的,这意味着会创建参数的副本,对参数在函数内部的修改不会影响原始变量。

在 `set_array` 函数中,当你修改 `array[0]` 时,会影响原始数组,因为在C中,数组是按引用传递的(数组名充当指向第一个元素的指针)。然而,在 `set_int` 函数中,你修改的是局部变量 `x`,它是 `main` 函数中 `a` 变量的副本。因此,修改不会影响原始的 `a` 变量。

要解决这个问题,你可以修改函数,接受指向变量的指针。以下是你的代码的更新版本,演示了指针的正确用法:

#include <stdio.h>

void set_array(int* array);
void set_int(int* x);

int main(void)
{
    int a = 10;
    int b[4] = { 0, 1, 2, 3 };
    set_int(&a);
    set_array(b);
    printf("%d %d\n", a, b[0]);
}

void set_array(int* array)
{
    array[0] = 22;
}

void set_int(int* x)
{
    *x = 22;
}
英文:
x = 22;
}

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:

#include &lt;stdio.h&gt;

void set_array(int* array);
void set_int(int* x);

int main(void)
{
   int a = 10;
   int b[4] = { 0, 1, 2, 3 };
   set_int(&amp;a);
   set_array(b);
   printf(&quot;%d %d\n&quot;, a, b[0]);
}

void set_array(int* array)
{
   array[0] = 22;
}

void set_int(int* x)
{
   *x = 22;
}


答案2

得分: 1

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

void set_int(int x) {
    x = 22;
}

int main(int argc, char** argv) {
    int a = 20;
    set_int(22);
    printf("%d\n", a);
}

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

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

int main(int arc, char** argv) {
    int a = 20;
    int* b = &a;
}

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

void set_int(int* x) {
    // 使用*操作符解引用我们的int*,y是内存中与a相同位置的原始整数
    int y = *x;
    // 因为我们正在使用a,现在我们可以将它设置为22
    y = 22;
}

int main(int arc, char** argv) {
    int a = 20;
    int* b = &a;
    set_int(b);
    // 这应该打印出22
    printf("%d\n", a);
    return 0;
}

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

void set_int(int* x) {
    *x = 22;
}

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

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

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

void set_int(int x) {
    x = 22;
}

int main(int argc, char** argv) {
    int a = 20;
    set_int(22);
    printf(&quot;%d\n&quot;, a);
}

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.

int main(int arc, char** argv) {
    int a = 20;
    int* b = &amp;a;
}

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.

void set_int(int* x) {
    // Dereference out int* with the * operator
    // y is out original integer a at the same spot in memory
    int y = *x;
    // Because we are using a we can now set it to out value of 22
    y = 22;
}

int main(int arc, char** argv) {
    int a = 20;
    int* b = &amp;a;
    set_int(b);
    // This should print 22
    printf(&quot;%d\n&quot;, a);
    return 0;
}

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

void set_int(int* x) {
    *x = 22;
}

And of course we can also compact our call:

int main(int argc, char** argv) {
    int a = 20;
    set_int(&amp;a);
    printf(&quot;%d\n&quot;, a);
    return 0;
}

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:

确定