我正在跟踪代码,但我不明白这些值是如何计算的。我的问题是关于代码旁边的注释。
我正在跟踪代码,并且我了解除 2 之外的所有部分。我在下面发布了这 2 部分。如果有人能帮助我,我会非常高兴。
#include <stdio.h>
int function1(int *m, int n)
{
*m = *m + n;
return(*m);
}
int function2(int n, int *m)
{
n = *m + 2;
return(n);
}
int main()
{
int x = 1, y = 3;
int *xptr = &x,*yptr = &y;
x = 1; y = 3;
y = function1(xptr,x);
printf("x = %d, Y = %d\n",x,y);//x=2 but why? shouldn't it be x=1? y=2
x = 1; y = 3;
x = function1(yptr,function2(2,yptr));
printf("x = %d, y = %d\n",x,y);//x=8 y=8 but why? shouldn't y=3?
return 0;
}
最佳答案
所以,在 function1 内部:
int function1(int *m, int n) {
*m = *m + n;
return(*m);
} /*
... */
y = function1(xptr,x);
n
保存x
值的副本,m
保存xptr
值的副本,它是x
的地址。*m
指的是m
所持有的地址内容- ...
*m = *m + n
即“赋值给x
地址的内容:x
地址的内容加上x
的值” .
x
的地址内容(也称为它的值)是 1。因此,我们将 1 + 1 分配给保存 x
值的地址内容。
关于c - 在 C 中,我在跟踪代码时遇到问题,并且不确定如何计算该值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55538616/