Can we change the value of an object defined with const through pointers?

前端 未结 10 1335
北恋
北恋 2020-11-22 10:49
#include 
int main()
{
    const int a = 12;
    int *p;
    p = &a;
    *p = 70;
}

Will it work?

10条回答
  •  深忆病人
    2020-11-22 11:11

    yes, you can make it done by using such code. but the code do not apply when when a is global (a gcc-compiled program gave me segmentation fault.)

    generally speaking, in beloved C, you can almost always find someway to hack things that are not supposed to be changed or exposed. const here being a example.

    But thinking about the poor guy(maybe myself after 6 months) maintains our code, I often choose not do so.

提交回复
热议问题