问题描述
嗨所有
我用以下代码进行了很多努力。只看到代码和解释,请告诉我你的输入
i有这样的代码
void main()
{
const int i = 10;
int * p =& i;
* p ++;
printf(%d,i);
}
它将显示的
值为10.
所以这里我的问题是让dennis变量声明const将存储在代码中(即只读memonry),意味着我们不能改变代码的内容吧?
这里我的问题是如何指针(她的指针p持有const变量i的地址)这里改变i的值。如果它存储在代码段中。
i已经问了很多人,但我没有得到答复..请尽可能多地给我发送解释,因为你的人可以
谢谢提前。
hi to all
i have stuggled a lot with the below code. just see the code and explanation and please tell me ur input on this
i have a code like this
void main()
{
const int i=10;
int *p=&i;
*p++;
Printf("%d",i);
}
value it will display is 10.
so here my question is accoriding to dennis the variable wich declare as const will be stored in codesigment(i.e read only memonry), means we cannot change the contents of the codesigment right??
here my question is then how come pointer(her pointer p wich is holding address of const variable i) here changing the value of "i" if it is stored in code segment.
i have asked lot of people but i didnt get the reply.. please send me as much as explanation as u people can
thanks in advance.
推荐答案
这里编译器将对象 - 左边是int *,右边是const int *,你不能在没有const_cast的情况下分配它。
Here compiler will object - to the left is int *, to the right is const int*, you cant assign it without const_cast.
我正在考虑c而不是c ++。在c中我将允许
i am taking about c not c++. in c i will allow
这篇关于怎么可能在c(const变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!