如何更改作为参数传递的变量的值,并同时使用该值进行数组操作。

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

How to change the value of a variable passed as an argument and also use the value for an array

问题

这是我的示例代码:

#include <stdio.h>

void Func(int a[], int b) {
    a[b] = 1;
    b += 5;
}

int main(void) {
    int a[10];
    int b = 0;
    printf("%d\n", b);
    Func(a, b);
    printf("%d\n", a[0]);
    printf("%d\n", b);
}

我想让程序打印出:

0
1
5

我尝试通过将b改为指针来更改函数:

void Func(int a[], int *b)

... 并写上*b += 5。另外,我改变了函数的调用方式:

int a[10];
int *b = 0;
// ...
Func(a, &b);

能够操纵b并使用它吗?

注:我尝试遵循这个帖子

英文:

This is my example code:

#include &lt;stdio.h&gt;

void Func(int a[], int b) {
    a[b] = 1;
    b += 5;
}

int main(void) {
    int a[10];
    int b = 0;
    printf(&quot;%d\n&quot;, b);
    Func(a, b);
    printf(&quot;%d\n&quot;, a[0]);
    printf(&quot;%d\n&quot;, b);
}

And I want the program to print:

0
1
5

I've tried changing the function by making b a pointer:

void Func(int a[], int *b)

... and by writing *b += 5. Also, I've changed the call of the function to:

int a[10];
int *b = 0;
// ...
Func(a, &amp;b);

Is it possible to manipulate b and to use it?


Note: I've tried following this post

答案1

得分: 3

void Func(int a[], int *b) {
   a[*b] = 1;
   *b += 5;
}

int main(void) {
    int a[10];
    int b = 0;

    printf("%d\n", b);    // 打印变量 b 的值
    Func(a, &b);          // 将变量 b 的地址传递给 Func
    printf("%d\n", a[0]);
    printf("%d\n", b);    // 再次打印变量 b 的值
}

Func 仍然有一个名为 b 的本地副本,但这个副本是一个指针,可以指向 main 中的 b 对象。这允许我们使用 *b += 5 修改指向的对象。

英文:

> c
&gt; void Func(int a[], int b){
&gt; a[b] = 1; // OK
&gt; b += 5; // useless
&gt; }
&gt;

The problem with this code is that Func is assigning a local copy of b, and this has no effect outside of the function.

To get the expected output:

#include &lt;stdio.h&gt;

void Func(int a[], int *b) {
   a[*b] = 1;
   *b += 5;
}

int main(void) {
    int a[10];
    int b = 0;

    printf(&quot;%d\n&quot;, b);    // print value of b
    Func(a, &amp;b);          // pass address of b to Func
    printf(&quot;%d\n&quot;, a[0]);
    printf(&quot;%d\n&quot;, b);    // print value of b again
}

Func still has a local copy named b, but this copy is a pointer which may point to the b object in main. This allows us to modify the pointed-to object with *b += 5.

答案2

得分: 1

你需要将ab用作指针,因为a是一个数组,而你想要在函数内修改b的值。

但是你不能直接将b直接传递给函数,这就是为什么要将其作为&amp;b发送,以提供变量存储的地址,这样你就不会复制b的值,而是直接改变它。

void foo(int *a, int *b) {
    a[*b] = 1;
    *b += 5;
}

int main(void) {
    int a[10] = {0};
    int b = 0;

    printf("b = [%i]\n", b);
    foo(a, &b);
    printf("a[0] = [%i]\n", a[b]);
    printf("b = [%i]\n", b);
}
英文:

For that you want to use a and b as a pointer, because a is an array and b you want to modify the value inside the function.

But you can't send directly b inside the function is why you to send as &amp;b to give the address where the variable is stored in that way you don't copy the value of b but directly change it.

void foo(int *a, int *b) {
    a[*b] = 1;
    *b += 5;
}

int main(void) {
    int a[10] = {0};
    int b = 0;

    printf(&quot;b = [%i]\n&quot;, b);
    foo(a, &amp;b);
    printf(&quot;a[0] = [%i]\n&quot;, a[b]);
    printf(&quot;b = [%i]\n&quot;, b);
}

huangapple
  • 本文由 发表于 2023年6月14日 23:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475161.html
匿名

发表评论

匿名网友

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

确定