Subtlety with C const pointers
It may be tricky to figure out what the difference is between these statements:
const char *foo; char * const bar; const char * const baz;
It would help to visualize this syntax like:
[const char] * [const baz];
in which the first group describes the pointee, while the second one defines the pointer.
const char *foo
foo is a mutable pointer pointing to an immutable string.
const char *foo = "you cannot change this content"; foo++; /* valid, foo now points to the second char */ foo[0] = 'X'; /* compile-time error */ free(foo); /* compile-time error */
Also interesting:
char *goo = (char *) foo; goo[0] = 'a'; /* runtime error, trying to modify read only data */
char * const bar
bar is an immutable pointer pointing to a mutable string.
char * const bar = "you can change this content"; bar[0] = 'L'; /* valid, the first word is now "Lou" */ bar++; /* compile-time error, frozen pointer value */ free(bar); /* valid */
const char * const baz
baz is an immutable pointer pointing to an immutable string.
const char * const baz = "I'm frozen!"; foo++; /* compile-time error */ foo[0] = 'X'; /* compile-time error */ free(foo); /* compile-time error */
