我在运行下面的代码时弄乱了指针和const:
int main()
{
const int test = 10;
int *ptr = &test;
printf("%d\n", test);
*ptr = 1;
printf("%d\n", test);
}
Run Code Online (Sandbox Code Playgroud)
在 Mac 上,结果为:
10
10
Run Code Online (Sandbox Code Playgroud)
在linux机器上:
10
1
Run Code Online (Sandbox Code Playgroud)
为什么更改值在 Mac 上不起作用?两台机器都使用 gcc 编译代码。