This creates a pointer to an existing line:
char *shift = "mondo";
This creates a new array of characters:
char shift[] = {'m', 'o', 'n', 'd', 'o', '\0'};
In the second case, you are allowed to change the characters, because these are the ones you just created.
In the first case, you simply point to an existing line that should never be changed. The details of where the string is stored depends on the specific compiler. For example, it can store a string in immutable memory. The compiler is also allowed to do tricks to save space. For instance:
char *s1 = "hello there"; char *s2 = "there";
s2 can actually point to the same letter 't', which is located at the seventh position of the line pointed to by s1 .
To avoid confusion, prefer to use constant pointers with string literals:
const char *shift = "mondo";
Thus, the compiler will tell you if you accidentally try to change it.
source share