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

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

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

问题

这是我的示例代码:

  1. #include <stdio.h>
  2. void Func(int a[], int b) {
  3. a[b] = 1;
  4. b += 5;
  5. }
  6. int main(void) {
  7. int a[10];
  8. int b = 0;
  9. printf("%d\n", b);
  10. Func(a, b);
  11. printf("%d\n", a[0]);
  12. printf("%d\n", b);
  13. }

我想让程序打印出:

  1. 0
  2. 1
  3. 5

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

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

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

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

能够操纵b并使用它吗?

注:我尝试遵循这个帖子

英文:

This is my example code:

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

And I want the program to print:

  1. 0
  2. 1
  3. 5

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

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

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

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

Is it possible to manipulate b and to use it?


Note: I've tried following this post

答案1

得分: 3

  1. void Func(int a[], int *b) {
  2. a[*b] = 1;
  3. *b += 5;
  4. }
  5. int main(void) {
  6. int a[10];
  7. int b = 0;
  8. printf("%d\n", b); // 打印变量 b 的值
  9. Func(a, &b); // 将变量 b 的地址传递给 Func
  10. printf("%d\n", a[0]);
  11. printf("%d\n", b); // 再次打印变量 b 的值
  12. }

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:

  1. #include &lt;stdio.h&gt;
  2. void Func(int a[], int *b) {
  3. a[*b] = 1;
  4. *b += 5;
  5. }
  6. int main(void) {
  7. int a[10];
  8. int b = 0;
  9. printf(&quot;%d\n&quot;, b); // print value of b
  10. Func(a, &amp;b); // pass address of b to Func
  11. printf(&quot;%d\n&quot;, a[0]);
  12. printf(&quot;%d\n&quot;, b); // print value of b again
  13. }

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的值,而是直接改变它。

  1. void foo(int *a, int *b) {
  2. a[*b] = 1;
  3. *b += 5;
  4. }
  5. int main(void) {
  6. int a[10] = {0};
  7. int b = 0;
  8. printf("b = [%i]\n", b);
  9. foo(a, &b);
  10. printf("a[0] = [%i]\n", a[b]);
  11. printf("b = [%i]\n", b);
  12. }
英文:

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.

  1. void foo(int *a, int *b) {
  2. a[*b] = 1;
  3. *b += 5;
  4. }
  5. int main(void) {
  6. int a[10] = {0};
  7. int b = 0;
  8. printf(&quot;b = [%i]\n&quot;, b);
  9. foo(a, &amp;b);
  10. printf(&quot;a[0] = [%i]\n&quot;, a[b]);
  11. printf(&quot;b = [%i]\n&quot;, b);
  12. }

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:

确定