Constant pointer : It means the pointer itself is constant i.e you can't point this pointer to some other variable.
A constant pointer is defined as : type * const pointer_name;
Lets see the example.
int main()
{
int val1 = 10, val2 = 20;
int * const ptr = &val1;
/* "int * const ptr" this is called a constant pointer. */
ptr = &val2;
/* This line will produce error. Because ptr is a constant pointer and hence can't point to another variable. */
printf("%d\n", *ptr);
return 0;
}
Pointer to constant : can change the address they point to but cannot change the value kept at those address.
A pointer to constant is defined as : const type * pointer_name;
Lets see the example.
int main()
int val = 10;
const int * ptr = &val;
/* "const int * ptr" this is called a pointer to constant. */
*ptr = 10;
/* This line will produce error. Because ptr is a pointer to constant and hence can't change the value */
printf("%d\n", *ptr);
return 0;
}