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

前端 未结 10 1384
北恋
北恋 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:31

    Bad, BAD idea.

    Also, the behavior is platform- and implementation-specific. If you're running on a platform where the constant is stored in non-writable memory, this obviously won't work.

    And, why on earth would you want to? Either update the constant in your source, or make it a variable.

提交回复
热议问题