Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a string literal in C++? [duplicate]

Tags:

c++

string

types

For example, what is the type of the string literal "Hello", const char[6] or const char* ?

like image 653
Belloc Avatar asked Mar 19 '13 18:03

Belloc


1 Answers

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.

like image 145
Joseph Mansfield Avatar answered Sep 16 '22 15:09

Joseph Mansfield