#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;
}
在这个程序中我可以理解情况1(通过将const变量的地址分配给非常量指针,我们可以改变变量值)。如果我将相同的概念应用于案例 2,我将无法工作。它给出了分段错误。是什么原因 ? case2 还有其他概念吗?这里我使用gcc编译器。