If this code is correct:
char v1[ ] = "AB";
char v2[ ] = {"AB"};
char v3[ ] = {'A', 'B'};
char v4[2] = "AB";
char v5[2] = {"AB"};
char v6[2] = {'A', 'B'};
char *str1 = "AB";
char *str2 = {"AB"};
Then why this other one is not?
char *str3 = {'A', 'B'};
To the best of my knowledge (please correct me if I'm wrong at any point) "AB" is a string literal and 'A' and 'B' are characters (integers,scalars). In char *str1 = "AB"; the string literal "AB" is defined and the char pointer is set to point to that string literal (to the first element). With char *str3 = {'A', 'B'}; two characters are defined and stored in subsequent memory positions, and the char pointer "should" be set to point to the first one. Why is that not correct?
In a similar way, a regular char array like v3[] or v6[2] can indeed be initialized with {'A', 'B'}. The two characters are defined, the array is set to point to them and thus, being "turned into" or treated like a string literal. Why a char pointer like char *str3 does not behave in the same way?
Just for the record, gcc compiler warnings I get are "initialization makes pointer from integer without a cast" when it gets to the 'A', and "excess elements in scalar initializer" when it gets to the 'B'.
Thanks in advance.
"AB"is a string literal as you point out, but{ 'A', 'B' }is not, nor any kind of array. It is data used to initialise the variable, so a) is of the wrong type, and b) more than one initialiser was provided.char *str3 = (char[]) {'A', 'B'};would work ... :-)char v3[ ] = {'A', 'B'};) 2) not allocating memory but only making them point to an already allocated unnamed string (char v1[ ] = "AB";).char v4[2] = "AB"; char v5[2] = {"AB"};are not correct. Because a character string is always terminated with a NUL byte. However, these two variable declarations are only allowing for 2 bytes while the actual string length is 3, not 2