C简易指针示例

时间:2014-11-26 00:09:43

标签: c++ c pointers

有人能解释我为什么程序的结果是“5 3”。我需要一个简短的步骤列表来显示程序的工作原理。请原谅我,如果我的问题太简单,我只是初学者。这是代码:

#include <stdio.h>
float x = 4.5;
float y = 2;
float proc(float z, float *x)
{
 *x *= y;
 return z + *x;
}
int main()
{
 float x, y, *z;
 x = 2.5; y = -2; z = &x;
 y = proc(y, z);
 printf("%f %f\n", x, y);
 return 0;
}

1 个答案:

答案 0 :(得分:0)

int main()
{
 float x, y, *z;
 x = 2.5; y = -2; z = &x;
 y = proc(y, z);  

 /*
  inside the proc function, you are passing -2 and the address of x in to proc.
  *x = *x * y // y = 2 which is the global variable defined earlier and *x = 2.5 which is the                  passed in variable.
  *x = 5 after the multiplication.
  z + *x = 3 will be returned and passed in to the variable y defined inside the main function.
  */
 printf("%f %f\n", x, y);
 return 0;
}