0

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????

  • Possible duplicate of [What is the difference between char s\[\] and char \*s?](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – Ana Oct 07 '19 at 13:07
  • A string literal is represented as a statically allocated array of `const char`. The length of that array is the number of characters in the literal, plus one for a nul terminator. So `char s[] = "Gorm"` creates `s` as an array of 5 `char`, and initialises elements of that array as a copy of the string literal i.e. `'G'`, `'o'`, `'r'`, `'m'`, and `'\0'`. – Peter Oct 07 '19 at 13:09
  • Possible duplicate of [What happens when a char array gets initialized from a string literal?](https://stackoverflow.com/questions/50938018/what-happens-when-a-char-array-gets-initialized-from-a-string-literal) – Sander De Dycker Oct 07 '19 at 13:11
  • You are correct to expect that a `const char*` can't be turned into a `char*`. The specific case of initializing a `char*` 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 the `const` though: https://godbolt.org/z/_Ycqg3 – Max Langhof Oct 07 '19 at 13:42
  • @MaxLanghof :in Peter comment i get that = symbol is not assignment but it is initialization so it takes constant elements of string literal "Gorm"and converts them to non constant char in s....but then in Max comment he talks about the same arrays but with them implicitly converted to pointers....do array syntax convert to pointer during initialization??!!i think not....so the pointer exception may just work on passing arrays to function or making logic comparison between arrays ... – code_for_ever Oct 07 '19 at 15:31
  • Arrays *decay* to pointers really easily, but arrays are *not* pointers. – Jesper Juhl Oct 07 '19 at 17:19
  • @code_for_ever : did you read the answers in the duplicate question I posted above - they should clear up your doubts. – Sander De Dycker Oct 08 '19 at 06:32
  • @SanderDeDycker yes i did and i edited the question ...they helped me understanding definition of literal string...but did no give explanation why string literal has type `char [5] const &` so i did not consider them duplicate – code_for_ever Oct 08 '19 at 09:13

1 Answers1

0

Although the your question is not clear I believe the primary issue is the idea that a string literal being a different type then a char array char[]. A string literal is not a string or a char array which are data type in c++, but is a syntax structure that is defined during the compile. Another example would be a integer literal 0b00000101 this represents an integer equal to 5 but is not an integer, just like "gorm" is not a string but represents a string (more accurately a char array) to the compiler. When declaring a char array and initializing it with a string literal like you do here: char s[] = "Gorm";. This tells the compiler to treat the string literal as a char array when initializing the char array s.

I believe looking into the concept of a literal and how it helps with initializing values in a programming langauge would be helpful. Link to Wikipedia page on Literals

  • i think the expression"syntax structure" should mean array of certain type so that compiler can find this structure and interpret it...your analogy to integral structure can be reformed to say that both `0b00000101` and 5 are integers in binary and decimal forms ...so "Gorm" and s are arrays in what forms??? – code_for_ever Oct 08 '19 at 09:26