关于const的问题 [英] question on const

查看:92
本文介绍了关于const的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


这是程序


#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

推荐答案



" user" <无** @ none.com>在留言新闻中写道:3f ****** @ usenet01.boi.hp.com ...

"user" <no**@none.com> wrote in message news:3f******@usenet01.boi.hp.com...
你好,

这是程序

#include stdio


#include< stdio.h>

int main(void)
{
const int num = 100;
int * ip;

ip =(int *)& num;
* ip = 200;
printf(" num of num)是%d(%d)\ n",num,* ip);


返回0;

}
输出:
num的值是100(200)
Hello,

Here is the program

#include stdio

#include <stdio.h>
int main(void)
{
const int num = 100;
int *ip;

ip = (int *)&num;
*ip = 200;
printf("value of num is %d(%d) \n", num, *ip);
return 0;
}
Output:
value of num is 100(200)




在我的系统上输出是


num的值是200(200)



On my system the output is

value of num is 200(200)

[..]


您好,


对不起。在我自己阅读帖子后,我觉得帖子本身就是不完整的。


当我尝试这个练习时,我希望代码在

编译阶段有错误(不是警告)。它没有,输出

让我想到当我指向相同的内存位置时,这可能有两个不同的值吗?我把输出检测到2 100s或2

200s。 ''num''变量指向某些地方????


我的问题是输出有何不同?该程序是否执行

未定义的行为?


谢谢


" user" <无** @ none.com>在留言新闻中写道:3f ****** @ usenet01.boi.hp.com ...
Hello,

I am sorry. After reading the post myself, I felt that the post is
incomplete in itself.

Well when I tried this exercise I expected the code to fail in the
compilation phase with an error ( not a warning). It did not, and the output
made me think how is this possible to have 2 different value when I am
pointing to the same memory location? I exptected the output to 2 100s or 2
200s. Is ''num'' variable pointing some where ????

My question is how is the output different? Is the program performing an
undefined behavior?

Thanks

"user" <no**@none.com> wrote in message news:3f******@usenet01.boi.hp.com...
你好,

这是程序

#include stdio

int main(void)
{
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 *)&num;
*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



我认为int * p找到一个新的地址,以便写一些东西给那个

地址。

如果你在这种情况下编写const int * ip,ip的地址就不能改变。

如果你为int * ip分配任何东西,它将被分配给一个地址

计算机会找到它来存储它。


-


------- --------------------

完成任务的时间!


删除(d * elete *)回复


" user" <无** @ none.com>在留言新闻中写道:3f ****** @ usenet01.boi.hp.com ...
I think the int* p find a new adress in order to write something to that
adress.
If you write const int* ip in that case the adress of ip can''t change.
If you assign anything to int *ip it will be assigned to an adress which the
computer will find in order to store it there.

--

---------------------------
Time to get it done!

Remove (d*elete*) to reply

"user" <no**@none.com> wrote in message news:3f******@usenet01.boi.hp.com...
你好,

这是程序

#include stdio

int main(void)
{
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 *)&num;
*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



这篇关于关于const的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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