C में Const Pointer: Pointer to Const vs Const Pointer आसान भाषा में
3 forms master करें: pointer to const (const int *p), const pointer (int *const p) और const pointer to const. कभी confuse न होने की simple trick.
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
const int *p और int *const p में क्या अंतर है?
const int *p pointer to const है: p से value change नहीं हो सकती लेकिन p कहीं और point कर सकता है. int *const p const pointer है: value change हो सकती है लेकिन p एक ही address पर locked है.
Function parameters में const char *str का क्या मतलब है?
यह promise करता है कि function string को सिर्फ read करेगा, कभी modify नहीं — strlen और printf जैसे library functions का standard pattern.
क्या const pointer को initialize करना ज़रूरी है?
हां. int *const p को declaration पर ही initialize करना ज़रूरी है क्योंकि बाद में उसे नया address assign नहीं हो सकता.