为什么允许使用memcpy指向它的指针更改const变量?
这段代码:
const int i=5;
int j = 0;
memcpy(&j, &i, sizeof(int));
printf("Source: i = %d, dest: j = %d\n", i,j);
j = 100;
memcpy(&i, &j, sizeof(int));
printf("Source: j = %d, dest: i = %d\n", j,i);
return 0;
Run Code Online (Sandbox Code Playgroud)
编译只是一个警告:
警告:传递'memcpy'的参数1从指针目标类型中丢弃'const'限定符[默认启用]
但确实运行得很好,并改变了const变量的值.