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

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

    It does indeed work with gcc. It didn't like it though:

    test.c:6: warning: assignment discards qualifiers from pointer target type

    But the value did change when executed. I won't point out the obvious no-no...

提交回复
热议问题