What's the difference between:
char *s1 = "dog";
char s2[] = "dog";
printf("%s", s1);
printf("%s", s2);
The first one doesn't print anything and the second prints dog as expected.
It seems to me they should be equivalent, as the right-hand side of char *s1 = "dog";
initializes a char array under the hood, and assigns the address of its first character to s1
. s2
is an array and thus contains the address to the first charcter in the string. So doesn't both s1
and s2
contain pointers to a the first character in a string "dog"? What am i missing? thanks!