A short guide to const.
char a;
const char b;
char *p = &a;
const char *q = &b;
char * const r = &a;
const char * const s = &b;
Step 1, read right to left, so for example
r is a const pointer to char.
Step 2, note that const can appear in two different places.
- the const to the right of the * means the pointer itself is const, that is (*p)++ may be allowed (modifying what it points to), but p++ isn't (modifying the pointer).
Also, such pointers MUST be declared and initialised in one step. Being const, you cannot assign them later on.
- the const to the left of the * means the thing being pointed to is const, that is p++ may be allowed, but (*p)++ isn't.
In the example above, the two RED declarations allow you to modify what the pointer points to, and the two GREEN ones do NOT allow you to modify what the pointer points to.
> I just want to confirm that my understanding is correct or not
It was good.