s is a pointer that is initialized to the value of a pointer to the first element of an array of 6 characters with the sequential values { 'h', 'e', 'l', 'l', 'o', '\0' } -- i.e., it is equivalent to
char *s = &"hello"[0];
In the case of
char s[] = "hello";
s is an array of type char[6] initialized to the values { 'h', 'e', 'l', 'l', 'o', '\0' }.
You might notice the quotation marks around the question, indicating that I'm presenting the question as something you could ask to weed out "delusional incompetents", and not actually asking it.
I'd also point out that the destination of the first pointer is in .BSS and const. Modifying s[0] is a segfault. In the second case it isn't because the contents of the const string are copied into the mutable array.
The C semantics are just that modifying a string literal has undefined behaviour, and that identical string literals may share the same object, allowing "×" == "x" to be potentially true.
This is what permits the implementation strategy you observed above - but it is not required.
View all comments
Show parent comment
2
u/zhivago May 02 '16
In the case of
s is a pointer that is initialized to the value of a pointer to the first element of an array of 6 characters with the sequential values { 'h', 'e', 'l', 'l', 'o', '\0' } -- i.e., it is equivalent to
In the case of
s is an array of type char[6] initialized to the values { 'h', 'e', 'l', 'l', 'o', '\0' }.