根据C标准,这是合法的东西吗? [英] Is this legal stuff as per C Standard?

查看:64
本文介绍了根据C标准,这是合法的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


在C ++中,如果试图用const_cast<取消const const变量的常量和/ b $ b,则它是未定义的行为。 > ;.


我想知道C中是否同样适用。

例如


const char * s =" abc";

后来,尝试做(char *)s合法吗?

我将不胜感激任何对标准的引用。


谢谢。

解决方案

Kelvin Moss写道:

Hi group ,在C ++中,如果试图用const_cast<>取消const变量的常量,那么它就是未定义的行为。

我想要的要知道C中是否同样有效。


const char * s =" abc" ;;
后来,试图做(char *)s合法?
我将不胜感激任何参考标准。

谢谢。



第6.7.3节说:如果尝试使用具有非const限定类型的const限定类型来修改定义的对象

,行为

未定义。


因此,如果你这样做,如果你试图修改abc,可能会发生任何事情。通过s。


Robert


谢谢。我很确定会是这种情况。


Robert Harris写道:

Kelvin Moss写道:

嗨组,

在C ++中,如果试图用const_cast<>取消const const变量的常量,那么它就是未定义的行为。

我想知道C中是否同样适用。


const char * s =" abc" ;;
后来,是试图做(char *)的合法吗?
我将非常感谢对标准的任何提及。

谢谢。
第6.7.3节说:如果尝试是使用非const限定类型的const限定类型修改定义的对象,行为
未定义。




这并不适用,因为abc不是const,也就是说,它不是一个用const限定类型定义的对象。请注意,

重要部分是对象的定义,而不是引用左值所具有的类型限定符



所以如果你如果你试图通过s修改abc
,可能会发生任何事情。

Robert




是的,但是未定义的行为是由于特殊的不能修改

字符串文字规则。


-Peter


-

拉出分裂来回复。


Hi group,

In C++ it''s undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?
I will appreciate any references to the Standard.

Thanks.

解决方案

Kelvin Moss wrote:

Hi group,

In C++ it''s undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?
I will appreciate any references to the Standard.

Thanks.


Section 6.7.3 says: "If an attempt is made to modify an object defined
with a const-qualified type with non-const qualified type, the behaviour
is undefined."

So if you do it, anything might happen if you try to modify "abc" through s.

Robert


Thanks. I was pretty sure that this would be the case.


Robert Harris wrote:

Kelvin Moss wrote:

Hi group,

In C++ it''s undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?
I will appreciate any references to the Standard.

Thanks.
Section 6.7.3 says: "If an attempt is made to modify an object defined
with a const-qualified type with non-const qualified type, the behaviour
is undefined."



That doesn''t apply, since "abc" is not const, which is to say, it is not
an object defined with a const qualified type. Notice that the
important part is what the object is defined as, not the type qualifiers
that the referring lvalue has.

So if you do it, anything might happen if you try to modify "abc"
through s.

Robert



Yes, but the undefined behavior is due to the special "can''t modify
string literals" rule.

-Peter

--
Pull out a splinter to reply.


这篇关于根据C标准,这是合法的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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