Skip to content

Instantly share code, notes, and snippets.

@liweinan
Created April 16, 2018 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liweinan/c83a59a07bf9822c85cfc29769088f12 to your computer and use it in GitHub Desktop.
Save liweinan/c83a59a07bf9822c85cfc29769088f12 to your computer and use it in GitHub Desktop.
把x的地址作为value传递,然后在foo内部,直接修改地址所在数据的值,就完成了对main里x的值的修改。
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