Search code examples
c++stringliteralsstring-literals

What is the type of a string literal?


I'm confused about the type of a string literal. Is it a const char * or a const char?


Solution

  • 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 NULs); if it was a pointer, it wouldn't work because it would always be the size of pointer on your system (minus one).