Pointers constants
- Siddharth Sharma
- Nov 8, 2024
- 3 min read
In C and C++, pointer constants refer to pointers that have constraints on either the pointer itself (where it can point) or on the value pointed to. These constraints can affect whether you can change the pointer’s address or the value it points to. Here’s a breakdown of the different kinds of pointer constants:
1. Constant Pointers (Pointer is Constant)
A constant pointer is a pointer that cannot change the memory address it points to after it’s initialized. In other words, the pointer itself is constant, but the value it points to can still be modified.
Declaration:
int x = 10;
int y = 20;
int* const ptr = &x; // const comes after the *, meaning the pointer is constantint* const ptr: This is a constant pointer to an integer. ptr must always point to x and cannot be changed to point to y, but you can change the value of x through ptr.
Example:
#include <stdio.h>
int main() {
int x = 10;
int y = 20;
int* const ptr = &x; // ptr is a constant pointer to an integer
*ptr = 15; // Modifying the value of x through ptr (allowed)
printf("x = %d\n", x); // Outputs: x = 15
// ptr = &y; // Error: ptr is a constant pointer and cannot point to a different address
return 0;
}Pointer to Constant (Value is Constant)
A pointer to a constant is a pointer that points to a constant value. The pointer can be changed to point to a different address, but you cannot modify the value at the address it currently points to.
Declaration:
const int* ptr = &x; // const comes before *, meaning the value pointed to is constantconst int* ptr: This is a pointer to a constant integer. You can change ptr to point to another integer, but you cannot modify the integer value it points to through ptr.
Example:
#include <stdio.h>
int main() {
int x = 10;
int y = 20;
const int* ptr = &x; // ptr is a pointer to a constant integer
// *ptr = 15; // Error: cannot modify the value of x through ptr
ptr = &y; // Allowed: ptr can point to a different address
printf("y = %d\n", *ptr); // Outputs: y = 20
return 0;
}3. Constant Pointer to Constant (Both Pointer and Value are Constant)
A constant pointer to a constant is a pointer that cannot be changed to point to a different address, and also cannot modify the value at that address. Both the pointer and the value it points to are constant.
Declaration:
const int* const ptr = &x; // const on both sides of *const int* const ptr: This is a constant pointer to a constant integer. ptr cannot point to a different address, and you cannot change the value it points to through ptr.
Example:
#include <stdio.h>
int main() {
int x = 10;
const int* const ptr = &x; // ptr is a constant pointer to a constant integer
// *ptr = 15; // Error: cannot modify the value at ptr
// ptr = &y; // Error: cannot change ptr to point to another address
printf("x = %d\n", *ptr); // Outputs: x = 10
return 0;
}Summary Table
Type | Syntax | Can Change Pointer? | Can Change Value Pointed To? |
Constant Pointer | int* const ptr | No | Yes |
Pointer to Constant | const int* ptr | Yes | No |
Constant Pointer to Constant | const int* const ptr | No | No |
Why Use Pointer Constants?
Constant pointers can help prevent accidental pointer reassignment in complex code, making it clear that a pointer should always reference the same variable.
Pointers to constants are useful for functions that should only read data without modifying it.
Constant pointers to constants provide a way to declare a fixed pointer to immutable data, often useful in configurations or for security-sensitive data.




Comments