What is the type of a string literal in C++? [duplicate]
For example, what is the type of the string literal "Hello", const char[6]
or const char*
?
The type of the string literal "Hello"
is "array of 6 const
char
".
Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n
const char
”, where n is the size of the string [...]
It can, however, be converted to a const char*
by array-to-pointer conversion. Array-to-pointer conversion results in a pointer to the first element of the array.
The Standard defines it as an "array of n const char
", so it's const char[n]
(n is the size of the string, including the terminating NUL byte).
Section 7, § 2.14.15:
A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration.