const变量重新分配 [英] const variable reassignment

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

问题描述

你好,

检查下面改变const i值的代码。

include< stdio.h>

int main()

{

const int i = 10;

int * p;

p =& i;

(* p)++;

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

返回0;

}


我们知道(* p)++与* p = * p + 1相同然后为什么我不被允许

直接为我分配新价值

即* p = 15;

不是* p = * p + 1正在为我重新获得新价值吗?

hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn''t *p=*p+1 is reassinging new value to i?

推荐答案

ra ******* @ gmail.com 写道:
你好,
检查以下代码改变const i值。
include< stdio.h>
int main ()
{
const int i = 10;
int * p;
p =& i;
(* p)++;
printf(" \ n%d",i);
返回0;
}
我们知道(* p)++与* p = *相同p + 1那么为什么我不是全部欠
直接为我分配新价值
即* p = 15;
是不是* p = * p + 1正在为i重新赋予新价值?
hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn''t *p=*p+1 is reassinging new value to i?




在你的代码中你说'我'应该是不变的,所以你期望什么?


八月



In your code you say that `i'' should be constant so what do you expect?

August



ra ******* @ gmail.com 写道:
你好,
检查下面改变const i值的代码。
include< stdio.h>
int main()
{
const int i = 10;
int * p;
p =& i;
(* p)++;
printf(" \ n%d,i);
返回0;
}
我们知道(* p)++与* p = * p + 1相同然后为什么我我不被允许
直接为我分配新价值
即* p = 15;
不是* p = * p + 1正在为i重新赋予新价值吗?
hello,
Check following code that changes const i value.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
Isn''t *p=*p+1 is reassinging new value to i?




通过指针修改const是一种未定义的行为。

确切地说,(* p)++与* p = * p + 1不同。



Modifying a const through a pointer is an undefined behaviour.
Exactly,(*p)++ is different from *p=*p+1.


ra*******@gmail.com 写道:
hello,
检查以下代码更改const i值。


这意味着它正在调用未定义的行为。

包含< stdio.h>
int main()
{
const int i = 10;
int * p;
p =& i;


你不编译生成警告吗?如果没有,你需要

将它产生的警告调到合理的水平。

(* p)++;
printf(" \ n%d,i);
返回0;
}
我们知道(* p)++与* p = * p + 1相同然后为什么我我不允许直接为我分配新价值
即* p = 15;


你不被允许,因为标准说它调用了undefined

行为,这意味着任何事情都可能发生。使用指针

增量也是不允许的,我不知道是什么让你觉得

是允许的。

难道不是* p = * p + 1正在为i重新赋予新的价值吗?
hello,
Check following code that changes const i value.
Which means it is invoking undefined behaviour.
include <stdio.h>
int main()
{
const int i=10;
int *p;
p=&i;
Doesn''t you compiler generate a warning for this? If not, you need to
turn up the warnings it generates to a sensible level.
(*p)++;
printf("\n %d",i);
return 0;
}

we know that (*p)++ is same as *p=*p+1 then why i am not allowed to
directly assign new value to i
i.e. *p=15;
You are not allowed to because the standard says it invoked undefined
behaviour, which means that anything can happen. Using the pointer to
increment it is also not allowed, and I don''t know what makes you think
it is allowed.
Isn''t *p=*p+1 is reassinging new value to i?




是的,这是不允许的。


如果它们永远不会被修改,那么只将它们声明为const,即

const的用途。如果他们将被修改,那么显然他们应该

不被声明为const。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



Yes, and it is not allowed.

Only declare things as const if they will never be modified, that is
what const is for. If they will be modified, then obviously they should
not be declared const.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


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

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