Professional Documents
Culture Documents
Const Pointer
Const Pointer
A constant pointer in C cannot change the address of the variable to which it is pointing, i.e., the
address will remain constant. Therefore, we can say that if a constant pointer is pointing to some
variable, then it cannot point to any other variable.
Syntax
const <type of pointer>* const <name of the pointer>;
Declaration for a constant pointer to a constant is given below:
const int* const ptr;
Let's understand through an example.
• We declare two variables, i.e., 'a' and 'b' with the values 10
#include <stdio.h>
and 90, respectively.
int main()
• We declare a constant pointer to a constant and then assign
{
the address of 'a'.
int a=10;
• We try to change the value of the variable 'a' through the
int b=90;
pointer 'ptr'.
const int* const ptr=&a;
• Then we try to assign the address of variable 'b' to the
*ptr=12;
pointer 'ptr'.
ptr=&b;
• Lastly, we print the value of the variable, which is pointed by
printf("Value of ptr is :%d",*ptr);
the pointer 'ptr'.
return 0;
}