The conversion
char* p = "foobar";
is deprecated in C++98/C++03, and has been removed (that is, §4.2/2 removed) in C++0x.
So, the code is not valid in C++0x.
However, MinGW g++ 4.4.1 still only emits a warning, not error.
C++98/C++03 §4.2/2 (which is removed in C++0x):
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. ]
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.
Best Answer
The first line
is valid in C, because "string literals", while used as initializer, boils down to the address of the first element in the literal, which is a pointer (to
char
).Related,
C11
, chapter §6.4.5, string literals,and then, chapter §6.3.2.1 (emphasis mine)
However, as mentioned in comments, in
C++11
onwards, this is not valid anymore as string literals are of typeconst char[]
there and in your case, LHS lacks theconst
specifier.OTOH,
is invalid (illegal) because,
1
is an integer constant, which is not the same type asint *
.