A constant pointer in C is a pointer whose stored address cannot be changed after initialization. This topic confuses many beginners because the word const can apply either to the pointer itself or to the data pointed to by the pointer. Those two meanings are not the same, and mixing them up leads to a lot of mistakes.
In this article, we will understand constant pointer in C clearly, learn its syntax, see how it differs from a pointer to constant, compare all common const-pointer combinations, and study examples that show what can and cannot be changed.
What is Constant Pointer in C?
A constant pointer in C is a pointer whose address value remains fixed after initialization. That means the pointer must keep pointing to the same memory location, but the data at that location may still be modified if the pointed data itself is not declared constant.
In simple words:
- the pointer cannot point somewhere else later
- the data may still be changeable, depending on the declaration
A constant pointer means the address is constant, not automatically the value stored at that address.
Syntax of Constant Pointer in C
The standard syntax is:
data_type * const pointer_name = &variable;Example:
int x = 10;
int y = 20;
int * const ptr = &x;Here, ptr must keep pointing to x. Reassigning it to &y is not allowed.
How Constant Pointer Works in C
When a pointer is declared as constant, the pointer variable itself becomes fixed after initialization. The compiler prevents reassignment of that pointer to another address.
- The pointer is initialized with a valid address.
- The pointer continues to hold that same address.
- The program can use the pointer to access that memory location.
- The pointer cannot be made to point elsewhere later.
This behavior is useful when the program logic requires a fixed target address.
Example of Constant Pointer in C
The following example shows what a constant pointer allows and disallows.
#include <stdio.h>
int main(void)
{
int x = 10;
int y = 20;
int * const ptr = &x;
*ptr = 50;
printf("%d\n", x);
/* ptr = &y; */
/* not allowed */
return 0;
}In this example:
*ptr = 50;is allowed because the pointed value is not constantptr = &y;is not allowed because the pointer itself is constant
Constant Pointer vs Pointer to Constant in C
This is the most important distinction in the topic.
| Declaration | Meaning | What cannot change |
|---|---|---|
int * const ptr | Constant pointer to int | The pointer address |
const int *ptr | Pointer to constant int | The pointed value through that pointer |
const int * const ptr | Constant pointer to constant int | Both address and value through that pointer |
The position of const changes the meaning. That is why reading declarations carefully matters in C.
Pointer to Constant in C
A pointer to constant means the pointer may point somewhere else later, but the value being accessed through that pointer should not be modified through that pointer.
const int *ptr = &x;Here, ptr can be redirected, but modifying *ptr is not allowed through that pointer.
Constant Pointer to Constant in C
This form makes both the address and the pointed value fixed through that pointer.
const int * const ptr = &x;In this case:
- the pointer cannot point elsewhere
- the pointed value cannot be changed through that pointer
How to Read Constant Pointer Declarations in C
A useful rule is to read from the variable name outward.
int * const ptr:ptris a constant pointer to intconst int *ptr:ptris a pointer to constant intconst int * const ptr:ptris a constant pointer to constant int
This reading method helps avoid confusion when declarations become more complex.
Where Constant Pointer is Used in C
- when a pointer should always refer to the same object
- when fixed memory-mapped hardware addresses are used in embedded systems
- when code clarity benefits from preventing accidental pointer reassignment
- when a function or module expects a stable pointer target
In embedded C, constant pointers are especially useful when working with hardware registers at fixed addresses.
Advantages of Constant Pointer in C
- prevents accidental pointer reassignment
- improves code safety and intention clarity
- useful for fixed-target memory access
- helps enforce stable program design in some modules
Common Mistakes with Constant Pointer in C
- confusing constant pointer with pointer to constant
- thinking
constalways makes the pointed value constant - placing
constin the wrong position - forgetting that a constant pointer must be initialized
| Mistake | Problem | Better understanding |
|---|---|---|
Confusing int * const ptr with const int *ptr | Wrong assumptions about what is fixed | Track whether const applies to pointer or data |
| Uninitialized constant pointer | Invalid declaration usage | Initialize it immediately at declaration |
| Assuming pointed value is always constant | Logical error | Check exact declaration carefully |
Best Practices for Constant Pointer in C
- Initialize constant pointers at declaration time.
- Use clear examples to distinguish const-data and const-pointer behavior.
- Choose the declaration that matches your real design intention.
- Prefer readability when working with multiple const combinations.
- Review declarations carefully in low-level and embedded code.
FAQs
What is constant pointer in C?
A constant pointer in C is a pointer whose stored address cannot be changed after initialization.
What is the difference between constant pointer and pointer to constant in C?
A constant pointer keeps the address fixed, while a pointer to constant protects the pointed value from modification through that pointer.
Can a constant pointer modify the value it points to?
Yes, if the pointed data itself is not declared constant. The pointer address is fixed, but the value may still be changeable.
Can a constant pointer be reassigned in C?
No. A constant pointer cannot be made to point to another address after it is initialized.
What does const int * const ptr mean in C?
It means ptr is a constant pointer to constant int. Both the pointer address and the pointed value are treated as fixed through that pointer.
Why are constant pointers useful in embedded C?
They are useful when code must keep pointing to fixed hardware addresses or stable memory locations without accidental pointer reassignment.