相关疑难解决方法(0)

为什么允许使用memcpy使用指向它的指针覆盖const变量?

为什么允许使用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变量的值.

c const memcpy

3
推荐指数
2
解决办法
2577
查看次数

标签 统计

c ×1

const ×1

memcpy ×1