C/C++ changing the value of a const

后端 未结 18 1843
醉梦人生
醉梦人生 2020-11-28 07:28

I had an article, but I lost it. It showed and described a couple of C/C++ tricks that people should be careful. One of them interested me but now that I am trying to repli

18条回答
  •  旧时难觅i
    2020-11-28 07:34

    this will create a runtime fault. Because the int is static. Unhandled exception. Access violation writing location 0x00035834.

    void main(void)
    {
        static const int x = 5;
        int *p = (int *)x;
        *p = 99;                //here it will trigger the fault at run time
    }
    

提交回复
热议问题