char *a= "ABC";
"ABC"
string is of const char*
type. How can its address be assigned to a char*
pointer?
Shouldn't it be an error?
char *a= "ABC";
"ABC"
string is of const char*
type. How can its address be assigned to a char*
pointer?
Shouldn't it be an error?
char *a = "ABC";
"ABC"
is of type char [4]
in C while it is of type const char [4]
in C++.
String literals in C have types of non-const arrays. From the C Standard (6.4.5 String literals):
The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.
Though string literals in C have types of non-const arrays they shall not be modified.
If the program attempts to modify such an array, the behavior is undefined.
In this connection consider for example the declaration of standard C function strchr
:)
char *
strchr(const char *
s, int c);
The function returns a pointer to the same string that as the parameter is defined with the qualifier const.
In C++ string literals have types of constant characters arrays.
From the C++ Standard
8 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 as defined below, and has static storage duration (3.7).