为什么可以将 const char* 分配给 char*? [英] Why is it possible to assign a const char* to a char*?

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

问题描述

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

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

  1. 我们如何将像 "hello" 这样的文字字符串分配给非 const char*,如下所示:

  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

  • 是像"hello"这样的文字字符串,它会占用我所有程序的内存,还是就像临时变量一样,在语句结束时会被销毁?

  • 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?

    推荐答案

    其实hello"的类型是char const[6].

    但问题的要点仍然是正确的——为什么 C++ 允许我们将只读内存位置分配给非 const 类型?

    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?

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

    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.

    也就是说,大多数编译器都可以配置为警告此类代码已弃用,甚至默认情况下这样做.此外,C++11 完全不允许这样做,但编译器可能还没有强制执行.

    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.

    对于Standerdese粉丝:
    [参考 1]C++03 标准:§4.2/2

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

    不是宽字符串文字的字符串文字(2.13.4)可以转换为pointer to char"类型的右值;宽字符串文字可以转换为指向 wchar_t 的指针"类型的右值.无论哪种情况,结果都是指向数组第一个元素的指针.仅当存在显式适当的指针目标类型时才考虑这种转换,而不是当一般需要从左值转换为右值时.[注意:此转换已弃用.见附录 D.] 为了在重载决议 (13.3.3.1.1) 中排序,这种转换被认为是数组到指针的转换,然后是限定转换 (4.4).[示例:abc"转换为指向 const char 的指针"作为数组到指针的转换,然后转换为指向 char 的指针"作为限定转换.]

    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 只是删除了上面的引用,这意味着它是 C++11 中的非法代码.

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

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

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

    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.

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

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