const pointer starting from basic.
1.int var; //we get a int variable named var
2.const int var; //we get a int variable named var and it is read-only(const)
3.int *p; //from 1, we get a int variable *p(because * is the dereferencing operator, thus p is pointer)
4.const int *p; //from 2,we get a int variable *p and it is read-only
5.int *const p; //from 3,we get a int variable *const p, and p is read-only, this can be read as "int *(const p)"
6.const int *const p //from 4 and 5, we get a int variable *const p and it is read-only, p is also read-only. this can be read as "const int *(const p)"
array in C is actually compiled as pointer. if we define a array like int ar[n], then we can get its elements value by dereferencing it as a pointer *(ar + i).
but is array really a pointer? yes and no.
when defining an array : int ar[n], we actually define it like 5------int *const ar. when we defining a const arr : const ar[n], we define it like 6------const int *const ar
they are just the same!
when passed as parameter to a function, they act just the same. neither can you redirect it, nor change the value it points to. you can not even change the value it not point to by using it. suppose :
void foo(const int ar[]) {
ar[i]++; //error
}
and
const int *const p;
(*(p + 1))++; //error too
however, you can change the value by using an other pointer
int *q = p;
(*(p + 1))++; //this is fine, you just get a warning
what an array can provide and a pointer can't is the size information : sizeof ar, but when passed to function, this information lost