const是真的常量吗? [英] Is const really const?

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

问题描述

大家好,


我对const关键字有疑问。我的理解是声明const的变量

不能被定义的模块修改。然后

考虑以下代码段:

main()

{

const int p = 5;

int * pp =& p;

++(* pp);

printf("%d \ nn",p);

}

令我惊讶它将p的值增加1,其中p必须是

常数。请澄清我的疑问。


谢谢和问候

Rohit

解决方案

Rohit kumar Chandel写道:


大家好,


我对const关键字有疑问。我的理解是声明const的变量

不能被定义的模块修改。然后

考虑以下代码段:

main()

{

const int p = 5;

int * pp =& p;

++(* pp);

printf("%d \ nn",p);

}

令我惊讶它将p的值增加1,其中p必须是

常数。请澄清我的疑问。



如果你没有得到至少三个上述警告,请转动你的

编译器''警告级别,所以它告诉你你的代码有什么问题。


是的C会让你逃脱你所写的,但结果

是不确定的行为。


-

Ian Collins。


" Rohit kumar Chandel" < ro **************** @ in.bosch.comwrites:


我对const关键字有疑问。我的理解是声明const的变量

不能被定义的模块修改。然后

考虑以下代码段:

main()

{

const int p = 5;

int * pp =& p;

++(* pp);

printf("%d \ nn",p);

}

令我惊讶它将p的值增加1,其中p必须是

常数。请澄清我的疑问。



在印度以外的大多数英语国家,怀疑不是

仅仅是问题的同义词。在像这样的国际论坛中,如果你写我有一个关于const关键字的问题,你将更有可能被理解。


在上面的程序中,声明

int * pp =& p;

违反约束条件。对于一个编译器,它会产生一个警告

消息:

警告:初始化从指针目标类型中丢弃限定符


你可以'合法地将const对象的地址分配给非const

指针对象。这条规则存在得恰到好处,以避免你看到的问题。但是,一旦编译器诊断出问题,就允许

继续编译程序。


增加编译器的警告级别,并且注意它产生的任何

警告信息。


如果你真的想修改一个const对象那么......好吧,首先是

all,你不应该尝试修改const对象;如果你需要修改

它,它首先不应该被声明为const。


但是这里有办法做什么你想做的事情:


#include< stdio.h / * printf * /

int main(void)

{

const int p = 5;

int * pp =(int *)& p; / *演员阵容抑制警告* /

++(* pp);

printf("%d \ n",p);

返回0;

}


有效的演员告诉编译器你知道你在做什么。

这几乎总是一个坏主意,程序的行为是未定义的。
未定义。编译器可以*假设* p永远不会被修改(因为

你答应不修改它)并打印5即使你已经设法

存储值6在它的记忆位置。


-

Keith Thompson(The_Other_Keith)< ks *** @ mib.org>

寻找圣地亚哥地区的软件开发工作。

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,Yes Minister


" Rohit kumar Chandel" ; < ro **************** @ in.bosch.com写道:


我对const关键字有疑问。我的理解是声明const的变量

不能被定义的模块修改。然后

考虑以下代码段:

main()

{

const int p = 5;

int * pp =& p;

++(* pp);

printf("%d \ nn",p);

}

令我惊讶它将p的值增加1,其中p必须是

常数。请澄清我的疑问。






int * pp =& p;


应该由编译器诊断出来。


你的,


-

Jean-Marc


Hi all,

I have a doubt in const keyword. My understanding says that a variable
declared const can not be modified by the module it is defined in. Then
consider the following code segment:
main()
{
const int p =5;
int* pp = &p;
++(*pp);
printf("%d\n",p);
}
To my surprise it increments value of p by one where p has to be a
constant. Please clarify my doubt.

Thanks and regards
Rohit

解决方案

Rohit kumar Chandel wrote:

Hi all,

I have a doubt in const keyword. My understanding says that a variable
declared const can not be modified by the module it is defined in. Then
consider the following code segment:
main()
{
const int p =5;
int* pp = &p;
++(*pp);
printf("%d\n",p);
}
To my surprise it increments value of p by one where p has to be a
constant. Please clarify my doubt.

If you didn''t get at least three warnings for the above, turn your
compiler''s warning level up so it tells you what is wrong with your code.

Yes C will let you get away with what you have written, but the result
is undefined behaviour.

--
Ian Collins.


"Rohit kumar Chandel" <ro****************@in.bosch.comwrites:

I have a doubt in const keyword. My understanding says that a variable
declared const can not be modified by the module it is defined in. Then
consider the following code segment:
main()
{
const int p =5;
int* pp = &p;
++(*pp);
printf("%d\n",p);
}
To my surprise it increments value of p by one where p has to be a
constant. Please clarify my doubt.

In most of the English-speaking world outside India, "doubt" is not
merely a synomym for "question. In international forums like this
one, you''d have a better chance of being understood if you write "I
have a question about the const keyword".

In your program above, the declaration
int *pp = &p;
violates a constraint. For one compiler, it produces a warning
message:
warning: initialization discards qualifiers from pointer target type

You can''t legally assign the address of a const object to a non-const
pointer object. This rule exists precisly to avoid the problem you''re
seeing. Once the compiler diagnoses the problem, though, it''s allowed
to continue to compile the program.

Increase the warning level of your compiler, and pay attention to any
warning messages it produces.

If you really want to modify a const object then ... well, first of
all, you shouldn''t try to modify a const object; if you need to modify
it, it shouldn''t have been declared const in the first place.

But here''s a way to do what you''re trying to do:

#include <stdio.h/* required for printf */
int main(void)
{
const int p = 5;
int *pp = (int*)&p; /* The cast suppresses the warning */
++(*pp);
printf("%d\n", p);
return 0;
}

The cast in effect tells the compiler that you know what you''re doing.
This is almost always a bad idea, and the behavior of the program is
undefined. A compiler could *assume* that p is never modified (since
you promised not to modify it) and print 5 even if you''ve managed to
store the value 6 in its memory location.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


"Rohit kumar Chandel" <ro****************@in.bosch.comwrites:

I have a doubt in const keyword. My understanding says that a variable
declared const can not be modified by the module it is defined in. Then
consider the following code segment:
main()
{
const int p =5;
int* pp = &p;
++(*pp);
printf("%d\n",p);
}
To my surprise it increments value of p by one where p has to be a
constant. Please clarify my doubt.

The line

int* pp = &p;

should have been diagnostized by the compiler.

Yours,

--
Jean-Marc


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

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