Why is it possible to assign a const char* to a char*?

后端 未结 5 1981
粉色の甜心
粉色の甜心 2021-01-04 03:27

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

  1. How can we assign a literal string like \"hel

相关标签:
5条回答
  • 2021-01-04 03:58

    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.

    0 讨论(0)
  • 2021-01-04 04:09

    The answer to your second question is that the variable s is stored in RAM as type pointer-to-char. If it's global or static, it's allocated on the heap and remains there for the life of the running program. If it's a local ("auto") variable, it's allocated on the stack and remains there until the current function returns. In either case, it occupies the amount of memory required to hold a pointer.

    The string "Hello" is a constant, and it's stored as part of the program itself, along with all the other constants and initializers. If you built your program to run on an appliance, the string would be stored in ROM.

    Note that, because the string is constant and s is a pointer, no copying is necessary. The pointer s simply points to wherever the string is stored.

    0 讨论(0)
  • 2021-01-04 04:10

    is literal string like "hello" will take memory in all my program all it's just like a temporary variable that will get destroyed when the statement ends.

    It is kept in programm data, so it is awaiable within lifetime of the programm. You can return pointers and references to this data from the current scope.

    The only reason why const char* is being cast to char* is comatiblity with c, like winapi system calls. And this cast is made unexplicit unlike any other const casting.

    0 讨论(0)
  • 2021-01-04 04:16

    Just use a string:

    std::string s("hello");
    

    That would be the C++ way. If you really must use char, you'll need to create an array and copy the contents over it.

    0 讨论(0)
  • 2021-01-04 04:19

    In your example, you are not assigning, but constructing. std::string, for example, does have a std::string(const char *) constructor (actually it's more complicated, but it doesn't matter). And, similarly, char * (if it was a type rather than a pointer to a type) could have a const char * constructor, which is copying the memory.

    I don't actually know how the compiler really works here, but I think it could be similar to what I've described above: a copy of "Hello" is constructed in stack and s is initialized with this copy's address.

    0 讨论(0)
提交回复
热议问题