为什么可以为char *赋值一个const char *? [英] Why is it possible to assign a const char* to a char*?

查看:570
本文介绍了为什么可以为char *赋值一个const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道例如hello const char * 的类型。所以我的问题是:


  1. 我们如何分配一个字符串hello到非 - const char * ,如下所示:

      char * s =hello; //hello是const char *的类型,s是char * 
    //我们知道从const char *到
    的转换无效


  2. 是一个字符串,如hello



解决方案

事实上,hello char const [6] p>

但问题的要点还是正确的 - 为什么C ++允许我们将一个只读存储器位置分配给非< - code> const type?



唯一的原因是向后兼容旧的C代码,不知道 const 。如果C ++在这里是严格的,它会破坏很多现有的代码。



也就是说,大多数编译器可以配置为警告代码不推荐使用,甚至默认情况下这样做。






对于Standerdese粉丝:

[Ref 1] C ++ 03标准:§4.2/ 2


不是宽字符串文字的字符串文字(2.13.4)可以转换为类型指向char的指针的右值;一个宽字符串文字可以转换为类型指向wchar_t的指针的右值。在任一情况下,结果都是指向数组的第一个元素的指针。仅当存在明确的适当的指针目标类型时才考虑此转换,而不是在通常需要从左值转换为右值时。 [请注意:此转换已弃用。见附件D.]为了在过载分辨率(13.3.3.1.1)中进行排序,这种转换被认为是一个数组到指针的转换,然后是一个资格转换(4.4)。 [示例:abc被转换为指向const char的指针,作为数组到指针的转换,然后转换为指向char的指针作为资格转换。 ]


C ++ 11只是删除上述引用,这意味着它是C ++ 11中的非法代码。



[参考2] C99标准6.4.5 / 5字符串文字 - 语义: b


在转换阶段7中,值为零的字节或代码附加到由字符串文字或字面量导致的每个多字节字符序列。多字节字符序列然后用于初始化静态存储持续时间和长度的数组,刚好足以包含该序列。对于字符串字面量,数组元素具有类型char,并且使用多字节字符序列的各个字节初始化;对于宽字符串字面量,数组元素具有类型wchar_t,并且使用宽字符序列初始化...



未指定这些数组是否有区别,具有适当的值。 如果程序尝试修改此类数组,则此行为未定义。



I know that for example "hello" is of type const char*. So my questions are:

  1. How can we assign a literal string like "hello" to a non-const char* like this:

    char* s = "hello";  // "hello" is type of const char* and s is char*
                        // and we know that conversion from const char* to
                        // char* is invalid
    

  2. Is a literal string like "hello", which will take memory in all my program, or it's just like temporary variable that will get destroyed when the statement ends?

解决方案

In fact, "hello" is of type char const[6].

But the gist of the question is still right – why does C++ allow us to assign a read-only memory location to a non-const type?

The only reason for this is backwards compatibility to old C code, which didn’t know const. If C++ had been strict here it would have broken a lot of existing code.

That said, most compilers can be configured to warn about such code as deprecated, or even do so by default. Furthermore, C++11 disallows this altogether but compilers may not enforce it yet.


For Standerdese Fans:
[Ref 1]C++03 Standard: §4.2/2

A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type "pointer to char"; a wide string literal can be converted to an rvalue of type "pointer to wchar_t". In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [Example: "abc" is converted to "pointer to const char" as an array-to-pointer conversion, and then to "pointer to char" as a qualification conversion. ]

C++11 simply removes the above quotation which implies that it is illegal code in C++11.

[Ref 2]C99 standard 6.4.5/5 "String Literals - Semantics":

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters...

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

这篇关于为什么可以为char *赋值一个const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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