在 page 15我的讲座幻灯片中有一个示例。
int x = 10;
increment_int(x); // can’t change the value of x
increment2_int(&x); // can change the value of x
我不明白为什么第一个函数 increment_int(x)
无法改变x的值。虽然我不知道这些函数到底是做什么的,但我猜它们正在为参数增加一些量。
最佳答案
- increment_int 是按值传递。如果函数increment_int改变 其参数的值,仅反射(reflect)在其本地副本中。 调用者看不到更改。
- increment2_int 通过引用传递。你传递 x 的地址而不是 该函数的 x 值。该函数改变的值 位于指定的地址,该地址也反射(reflect)在调用方。
关于c - 为什么要使用变量的地址来改变变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583918/