In C/C++, a constant pointer can’t change the address of the variable it’s pointing to, so the address stays the same. As a result, we can conclude that if a constant pointer points to one variable, it cannot point to another variable.
Syntax of Constant Pointer:
*const ;
How to declare a constant pointer?
int *const ptr;
Let’s look at an example to better grasp the constant pointer.
#include <stdio.h> int main() { int a=1; int b=2; int *const ptr; ptr=&a; ptr=&b; printf("Value of ptr is :%d",*ptr); return 0; }
In the above code:
- We declare two variables, i.e., a and b with values 1 and 2, respectively.
- We declare a constant pointer.
- First, we assign the address of variable ‘a’ to the pointer ‘ptr’.
- Then, we assign the address of variable ‘b’ to the pointer ‘ptr’.
- Lastly, we try to print the value of the variable pointed by the ‘ptr’.
Output:
Compilation failed due to following error(s)
main.c In function 'main':
main.c:16:8: error: assignment of read-only variable 'ptr'
ptr = &a;
^
main.c:17:8: error: assignment of read-only variable 'ptr'
ptr = &b;
^
We can see from the output that the above code results in the error “assignment of read-only variable ‘ptr’.” It signifies that the value of the variable ‘ptr’ held by ‘ptr’ cannot be modified. We are altering the value of ‘ptr’ from &a to &b in the given code, which is not feasible with constant pointers. As a result, we can claim that a constant pointer pointing to one variable cannot point to another.
Pointer to Constant:
A pointer to a constant is a pointer that cannot modify the value of the variable to which the pointer points. These pointers’ addresses can be modified, but the value of the variable to which the pointer points cannot.
Syntax of Pointer to Constant:
const *
The Declaration of a pointer to constant is given below:
const int* ptr;
- First, we write the code where we are changing the value of a pointer
#include <stdio.h> int main() { int a=1000; int b=2000; const int* ptr; ptr=&a; ptr=&b; printf("Value of ptr is :%u",ptr); return 0; }
In the above code:
- We declare two variables, i.e., a and b with the values 1000 and 2000 respectively.
- We declare a pointer to constant.
- First, we assign the address of variable ‘a’ to the pointer ‘ptr’.
- Then, we assign the address of variable ‘b’ to the pointer ‘ptr’.
- Lastly, we try to print the value of ‘ptr’.
Output:
Value of ptr is : 23333564
The preceding code executes correctly, and the value of ‘ptr’ is displayed in the output.
Now we’ll create the code to change the value of the variable that the pointer points to.
#include <stdio.h> int main() { int a=100; int b=200; const int* ptr; ptr=&b; *ptr=300; printf("Value of ptr is :%d",*ptr); return 0; }
In the code above:
- We set the values of two variables, ‘a’ and ‘b,’ to 100 and 200, respectively.
- We declare a constant pointer.
- The address of the variable ‘b’ is assigned to the pointer ‘ptr’.
- Then, using the pointer ‘ptr,’ we try to change the value of the variable ‘b.’
- Finally, we try to display the value of the variable referenced by the ‘ptr’ pointer.
Output:
main.c: In function 'main':
main.c:17:9: error: assignmet of read-only location '*ptr'
*ptr=300
The error “assignment of read-only location ‘*ptr’” may be seen in the code above. We can’t modify the value of the variable to which the pointer is referring because of this issue.
Constant Pointer to a Constant:
A pointer that is a mixture of the above two pointers is a constant pointer to a constant. It can’t change the address of the variable it’s pointing to, nor can it change the value that’s been assigned to it.
Syntax:
const * const ;
The following is the declaration for a constant reference to a constant:
const int* const ptr;
Let’s have a look at an example.
#include <stdio.h> int main() { int a=10; int b=90; const int* const ptr=&a; *ptr=12; ptr=&b; printf("Value of ptr is :%d",*ptr); return 0; }
In the code above:
- We set the values of two variables, ‘a’ and ‘b,’ to 10 and 90, respectively.
- The address of ‘a’ is assigned once we declare a constant pointer to a constant.
- Using the pointer ‘ptr,’ we try to modify the value of the variable ‘a.’
- Then we try to assign variable ‘b”s address to the pointer ‘ptr’.
- Finally, we print the value of the variable referenced by the ‘ptr’ pointer.
Output:
Compilation failed due to following error(s)
main.c In function 'main':
main.c:17:9: error: assignment of read-only location 'ptr'
*ptr = 12;
^
main.c:18:6: error: assignment of read-only variable 'ptr'
ptr = &b;
^
If you want to improve this post connect us.