Why is it allowed to overwrite a const variable using a pointer to it using memcpy?

后端 未结 4 426
难免孤独
难免孤独 2021-01-21 04:19

Why is it allowed to change a const variable using a pointer to it with memcpy?

This code:

const int i=5;
int j = 0;
memcpy(&j, &i, sizeof(int));         


        
4条回答
  •  既然无缘
    2021-01-21 04:58

    While 'officially' it's undefined in reality it's very much defined - you will change the value of the const variable. Which raises the question why it's const to begin with.

提交回复
热议问题