不使用volatile的情况下: #include<stdio.h> #include<stdlib.h> int main() { const int n = 10; int*p = (int*)&n; *p = 20; printf("%d\n", n); printf("%d\n", *p); system("pause"); return 0; } 结果:
20
20
请按任意键继续. . .
#include<stdio.h> #include<stdlib.h> int main() { volatile const int n = 10; int*p = (int*)&n; *p = 20; printf("%d\n", n); printf("%d\n", *p); system("pause"); return 0; }
结果:
10
20
请按任意键继续. . .