怎么可能在c(const变量) [英] how this is possible in c(const variable)

查看:78
本文介绍了怎么可能在c(const变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

我用以下代码进行了很多努力。只看到代码和解释,请告诉我你的输入


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.

推荐答案

当你执行* p ++时,你获取p指向的值并使p指向

下一个项目(无论什么类型)。换句话说:你并没有试图将const int i的值增加到
。尝试做(* p)++并看看会发生什么。


亲切的问候,


Jos
When you do *p++ you fetch the value of what p points to and make p point to
the next item (whatever its type). In other words: you''re not trying to increment
the value of const int i. Try to do (*p)++ and see what happens.

kind regards,

Jos



int * p =& i;
int *p=&i;



这里编译器将对象 - 左边是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.



这里编译器将对象 - 左边是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变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆