Created
April 16, 2018 14:35
-
-
Save liweinan/c83a59a07bf9822c85cfc29769088f12 to your computer and use it in GitHub Desktop.
把x的地址作为value传递,然后在foo内部,直接修改地址所在数据的值,就完成了对main里x的值的修改。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void foo(int *x) { | |
*x = 2; // x的地址作为参数被传递,x的地址是pass by value,在foo的stack里被复制了一份,然后我们修改对应地址的值。 | |
} | |
int main() { | |
int x = 1; | |
foo(&x); | |
printf("x: %d\n", x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment