🔴 传值改不动,传地址改得动
(本条路线统一用 gcc -std=c11 -O0 编译)两个函数都想把外面的变量改成 99:
#include <stdio.h>
void try_set(int x) { /* 传值:改的是副本 */
x = 99;
}
void really_set(int *p) { /* 传地址:改得动 */
*p = 99;
}
int main(void) {
int a = 1, b = 1;
try_set(a);
really_set(&b);
printf("%d/%d\n", a, b);
return 0;
}
全部评论