With this declaration:
char foo[6] = "shock";
Variable foo
is type array of char and it containes 6 non-const chars. The string literal contains const chars which are copied into the array on initialization.
While with this declaration:
char* bar = "shock"; //error
Variable bar
is type pointer to char. You are trying to make it point to the address of "shock"
which is a string literal containing const char
.
You can't point a pointer to non-const char at a const char.
So you must do this:
const char* bar = "shock";`
foo
is type array of char whilebar
is type pointer to char.foo
is a copy of the const"shock"
.bar
is a pointer to the const"shock"
. You can decide whether the copy will be const or not.