I am reading c++ programming language book and i am learning about pointer to char,
i found this code char s[] = "Gorm";
which reminds me that string literal is converted implicitly to const char * .
so i felt confused that lhs and rhs are not of same type.
i used online compiler and code from here:Is it possible to print a variable's type in standard C++?
to get idea about how compiler see type of lhs and rhs,
then i found that lhs is seen as char[5] while rhs is seen as char[5] const &
i can interpret lhs ,
but i can not understand what is "constant lvalue reference to array of 5 char" or
even after implicit conversion of lhs char s[] to char* s ,
what is "constant lvalue reference to non const pointer to non const char" ??
is not lvalue reference by definition constant "it refers only to value initializing it"???
so why do we need const before "&"???
and then how can lhs and rhs of different types be assigned??????
the suggested answer is really helpful in understanding how char s[] is initialized from string literal "it is language rule" .
but it does not answer the part of question regarding the expression"char [5] const &"???
what is the meaning of const & in this strange expression?!!!
what is const referring to???
is it referring to the lvalue reference itself ???"reference is const by definition and we can not change value of reference after initialization"???
or is it referring to the element of the array const char so that string literal array can not change ????
but in this case const should have been put before char or after char not before & ?????!!!
or const is referring to the type char[5] and in this case what is its importance ????
normally array can not be changed regarding size or type after definition????
const char. The length of that array is the number of characters in the literal, plus one for a nul terminator. Sochar s[] = "Gorm"createssas an array of 5char, and initialises elements of that array as a copy of the string literal i.e.'G','o','r','m', and'\0'.const char*can't be turned into achar*. The specific case of initializing achar*from string literals is an exception in many compilers because of the ancient legacies of C++. A modern compiler should at least give out a warning that you probably want to keep theconstthough: godbolt.org/z/_Ycqg3