char *p = "string"; //creates pointer to constant string
char p[] = "string"; //just an array with "string"
I’m just a bit confused about why does it in the first example create a pointer to a constant string? Shouldn’t it be just a pointer refering to a place in memory with “string”?
It is unfortunately legal in C (and in C++03, for compatibility). But any attempt to modify the string literal via the pointer will result in Undefined behavior. So, better always assign the string literal to a
const char*