Reputation: 13143
I'm confused about the type of a string literal. Is it a const char *
or a const char
?
Upvotes: 25
Views: 4062
Reputation: 39415
Ordinary string literals are of type const char[n]
, i.e. "array of n const char
", where n is the number of characters in the string, including the null terminator.
For example, the string literal "foo"
is an lvalue of type const char[4]
(n is 4
due to the null terminator at the end of the string).
It would be valid to write:
const char* str = "foo";
... because of array-to-pointer conversion, but the type of "foo"
is still const char[4]
.
There are other kinds of string literals with different types. Here is a comparison, taken from Table 12: String literals in the C++ standard:
Kind | Type | Example |
---|---|---|
ordinary string literal | array of n const char |
"ordinary string" |
wide string literal | array of n const wchar_t |
L"wide string" |
UTF-8 string literal | array of n const char8_t |
u8"UTF-8 string" |
UTF-16 string literal | array of n const char16_t |
u16"UTF-16 string" |
UTF-32 string literal | array of n const char32_t |
u32"UTF-32 string" |
Ordinary raw string literals are also of type const char[n]
. For example, a raw UTF-8 string literal u8R"(raw)"
is of type const char8_t[4]
.
In C++98, the type of string literals is non-const
, same as in C. This behavior was deprecated in C++03 and the type was made const
in C++11. However, string literals are non-const
in C to this day.
For all intents and purposes, you should treat string literals as if they are const
. Even if you're using an old version of C++, or you're using C, you are not allowed to modify the string, so adding an "unnecessary" const
can prevent bugs.
Upvotes: 0
Reputation: 70213
"Hello world"
is const char[12]
(eleven characters plus '\0'
terminator).
L"Hello world"
is const wchar_t[12]
.
Since C++14, "Hello world"s
is std::string
.
Since C++17, "Hello world"sv
is std::string_view
.
Also note the u8""
, u""
and U""
string literal notations added by C++11, which specify UTF-8, UTF-16 and UTF-32 encoding, respectively. The encoding of non-qualified string literals (i.e. ""
and L""
) is (and always was) implementation-defined.
Upvotes: 10
Reputation: 75130
It is a const char[N]
(which is the same thing as char const[N]
), where N
is the length of the string plus one for the terminating NUL
(or just the length of the string if you define "length of a string" as already including the NUL
).
This is why you can do sizeof("hello") - 1
to get the number of characters in the string (including any embedded NUL
s); if it was a pointer, it wouldn't work because it would always be the size of pointer on your system (minus one).
Upvotes: 32