Is it UB to Modify where pointer points, when original data is const?

后端 未结 2 1035
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 00:41

Would it be undefined behavior to change where a pointer points, when its data is const? Example:

const char* p = \"foo\";
p = \"boo\";

I belie

2条回答
  •  难免孤独
    2021-01-26 01:18

    The code in the first snippet is 100% correct. You have a pointer to const p, which you repoint to something else. All good and in mint condition.

    The second piece of code is ill-formed. You can't modify an object after removing the constness, if original object was const-qualified (which string literal is).

提交回复
热议问题