C 简易指针示例

C easy pointer example

本文关键字:指针 易指针      更新时间:2023-10-16

谁能解释为什么程序的结果是"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 %fn", x, y);
 return 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 %fn", x, y);
 return 0;
}