Const Pointer in C: Pointer to Const vs Const Pointer Explained
Master the 3 forms: pointer to const (const int *p), const pointer (int *const p) and const pointer to const. Simple trick to never confuse them again.
The 3 forms of const with pointers
1.
const int *p — pointer to const (data locked)
2.
int *const p — const pointer (address locked)
3.
const int *const p — const pointer to const (both locked)The trick to never confuse them
const int *p→ "p is a pointer to an int that is const" → value locked, pointer free.int *const p→ "p is a const pointer to an int" → pointer locked, value free.const int *const p→ "p is a const pointer to a const int" → both locked.
const touches on its left (or right if nothing on left) is what gets locked.Form 1: Pointer to const
#include <stdio.h>
int main() {
int a = 10, b = 20;
const int *p = &a;
// *p = 15; // ERROR: value is locked through p
p = &b; // OK: pointer can move to another address
printf("%d\n", *p);
return 0;
}
Most used form. Every safe read-only function parameter is a pointer to const: size_t strlen(const char *s);
Form 2: Const pointer
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *const p = &a;
*p = 15; // OK: value can change
// p = &b; // ERROR: pointer address is locked
printf("%d\n", a);
return 0;
}
A const pointer must be initialized at declaration, exactly like any const variable — because you cannot re-point it later.
Form 3: Const pointer to const
int a = 10, b = 20;
const int *const p = &a;
// *p = 15; // ERROR: value locked
// p = &b; // ERROR: pointer locked
printf("%d\n", *p); // reading is the only allowed operation
Maximum protection: you can only read through p. Used for fixed lookup tables and hardware register maps.
Side-by-side summary
| Declaration | Change value (*p = x) | Change pointer (p = &y) |
|---|---|---|
int *p | ✅ Yes | ✅ Yes |
const int *p | ❌ No | ✅ Yes |
int *const p | ✅ Yes | ❌ No |
const int *const p | ❌ No | ❌ No |
Pointers के साथ const के 3 forms
1.
const int *p — pointer to const (data locked)
2.
int *const p — const pointer (address locked)
3.
const int *const p — const pointer to const (दोनों locked)कभी confuse न होने की trick
const int *p→ "p एक pointer है ऐसे int का जो const है" → value locked, pointer free.int *const p→ "p एक const pointer है int का" → pointer locked, value free.const int *const p→ "p एक const pointer है const int का" → दोनों locked.
const अपने left में (left में कुछ न हो तो right में) जिसे touch करता है, वही lock होता है.Form 1: Pointer to const
#include <stdio.h>
int main() {
int a = 10, b = 20;
const int *p = &a;
// *p = 15; // ERROR: p के through value locked है
p = &b; // OK: pointer दूसरे address पर जा सकता है
printf("%d\n", *p);
return 0;
}
सबसे ज़्यादा use होने वाला form. हर safe read-only function parameter pointer to const होता है: size_t strlen(const char *s);
Form 2: Const pointer
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *const p = &a;
*p = 15; // OK: value change हो सकती है
// p = &b; // ERROR: pointer का address locked है
printf("%d\n", a);
return 0;
}
const pointer को declaration पर ही initialize करना ज़रूरी है, बिल्कुल किसी const variable की तरह — क्योंकि बाद में re-point नहीं कर सकते.
Form 3: Const pointer to const
int a = 10, b = 20;
const int *const p = &a;
// *p = 15; // ERROR: value locked
// p = &b; // ERROR: pointer locked
printf("%d\n", *p); // सिर्फ reading allowed है
Maximum protection: p से सिर्फ read कर सकते हैं. Fixed lookup tables और hardware register maps के लिए use होता है.
Side-by-side summary
| Declaration | Value change (*p = x) | Pointer change (p = &y) |
|---|---|---|
int *p | ✅ हां | ✅ हां |
const int *p | ❌ नहीं | ✅ हां |
int *const p | ✅ हां | ❌ नहीं |
const int *const p | ❌ नहीं | ❌ नहीं |
Frequently Asked Questions
What is the difference between const int *p and int *const p?
const int *p is a pointer to const: the value cannot change through p but p can point elsewhere. int *const p is a const pointer: the value can change but p is locked to one address.
What does const char *str mean in function parameters?
It promises the function will only read the string and never modify it — the standard pattern used by library functions like strlen and printf.
Does a const pointer need initialization?
Yes. int *const p must be initialized at declaration because it cannot be assigned a new address later.