The string literal itself has array type. So in the first example you gave, there are actually two arrays involved. The first is the array containing the string literal and the second is the array arr
that you're declaring. The characters from the string literal are copied into arr
. The C++11 wording is:
A char
array (whether plain char
, signed char
, or unsigned char
), char16_t
array, char32_t
array, or wchar_t
array can be initialized by a narrow character literal, char16_t
string literal, char32_t
string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces. Successive characters of the value of the string literal initialize the elements of the array.
In the second example, you are letting the string literal array undergo array-to-pointer conversion to get a pointer to its first element. So your pointer is pointing at the first element of the string literal array.
However, note that your second example uses a feature that is deprecated in C++03 and removed in C++11 allowing a cast from a string literal to a char*
. For valid C++11, it would have to instead be:
const char* cp = "Hello";
If do use the conversion to char*
in C++03 or in C, you must make sure you don't attempt to modify the characters, otherwise you'll have undefined behaviour.