我想通过使用指针来改变常量的值.
请考虑以下代码
int main()
{
const int const_val = 10;
int *ptr_to_const = &const_val;
printf("Value of constant is %d",const_val);
*ptr_to_const = 20;
printf("Value of constant is %d",const_val);
return 0;
}
正如预期的那样,常量的值被修改.
但是当我尝试使用全局常量的相同代码时,我遇到了运行时错误.Windows崩溃记者正在开放.在此语句"*ptr_to_const = 20;"中打印第一个printf语句后,可执行文件暂停
请考虑以下代码
const int const_val = 10;
int main()
{
int *ptr_to_const = &const_val;
printf("Value of constant is %d",const_val);
*ptr_to_const = 20;
printf("Value of constant is %d",const_val);
return 0;
}
该程序使用codeblocks IDE在mingw环境中编译.
谁能解释一下发生了什么?