问题描述
你好,
这是程序
#include stdio
int main (无效)
{
const int num = 100;
int * ip;
ip =(int *)& num;
* ip = 200;
printf(" num的值是%d(%d)\ n", num,* ip);
}
输出:
num的值是100(200)
输出显示* ip已更改且''num''未更改。当这两个指向相同的内存位置时,这是怎么回事?我疯狂的猜测
表示这个技巧是在编译器级别处理的。我是否正确?
即使内存位置可访问且内容已更改,
''const integer''也不受影响。
谢谢
Hello,
Here is the program
#include stdio
int main(void)
{
const int num = 100;
int *ip;
ip = (int *)#
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);
}
Output:
value of num is 100(200)
The output says that *ip is changed and ''num'' is unchanged. How is this
possible when both of them point to the same memory location? My wild guess
says that this trick is handled at the compiler level. Am I correct?
Even when the memory location is accessable and the contents changed the
''const integer'' is unaffected.
Thanks
推荐答案
在我的系统上输出是
num的值是200(200)
On my system the output is
value of num is 200(200)
[..]
这篇关于关于const的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!