First, you need to know that when you declare a pointer variable like int *ptr = &num,
int *ptr = (int) ptr.
For example, if you declare int p, q;, p is a pointer variable holding an int, and q is an int.
- const int *
const int *ptr means the same as (const int) *ptr. That is,
the ptr pointer variable stores the address of a const int variable.
So
const int a = 6;
int *p = &a;
results in an ERROR.
p is a pointer variable that holds an int variable, not a pointer variable that holds a const int.
So
const int *p = &a;
you have to declare it like this for it to work properly.
- int *const
int *const ptr means the same as int (*const ptr).
It's a const pointer holding an int pointer.
That is, you can change the contents at the address but you can't change the address.
p = &a;
int num = 5;
p = # is not allowed, but
*p = 8; is allowed.