#include <stdio.h>
int main() {
// case 1
const int a = 10;
int * ptr1 = &a;
*ptr1 = 11;
printf("%d\n",*ptr1);
// case 2
const static int b = 10;
static int * ptr2 = &b;
*ptr2 = 11;
printf("%d\n",*ptr2);
return 0;
}
在这个程序中,我可以理解第一种情况(通过将const变量的地址分配给非常数指针,我们可以更改变量值).如果我把同样的概念应用于 case 2,我就不会工作了.给出了分段故障.原因是什么?对于 case 2还有其他的概念吗?这里我用的是GCC编译器.