C Programming में Pointer Arithmetic का उपयोग कैसे करें?
How to use Pointer Arithmetic in C Programming?
C Programming में Pointer Arithmetic का उपयोग कैसे करें?: पिछले Tutorial में हमने यह समझा था कि Pointer क्या होता है, Pointer को कैसे declare, initialize और use किया जाता है। हमने यह भी जाना था कि Pointer memory में मौजूद किसी variable के address को store करता है।
C Programming में Pointer Arithmetic का उपयोग कैसे करें? (पूरी जानकारी हिंदी में)
इस tutorial में हम एक और महत्वपूर्ण concept सीखेंगे – Pointer Arithmetic यानी Pointer के साथ अंकगणितीय (Arithmetic) operations करना।
Pointer भी एक Variable है
सबसे पहले यह समझ लीजिए कि Pointer भी एक variable ही होता है, फर्क सिर्फ इतना है कि:
| सामान्य Variable | Pointer Variable |
|---|---|
| actual data store करता है | किसी variable का address store करता है |
उदाहरण:
int a = 10; // a में value 10 store है
int *p = &a; // p में a का address store है
Pointer Arithmetic क्या है?
जैसे हम सामान्य variables पर +, -, ++, -- और comparison operators का use करते हैं, वैसे ही Pointer के साथ भी कर सकते हैं, लेकिन कुछ limit के साथ।
Pointer के साथ आप निम्न operations कर सकते हैं:
| Operation | Meaning |
|---|---|
ptr++ | Pointer को अगले memory location पर ले जाए |
ptr-- | Pointer को पिछले memory location पर ले जाए |
ptr + n | Pointer में n locations आगे जाए |
ptr - n | Pointer में n locations पीछे जाए |
ptr1 == ptr2 | Compare करें कि क्या दोनों same memory address को point करते हैं |
Important Rule
Pointer arithmetic करते समय data type बहुत महत्वपूर्ण है।
क्योंकि Pointer हमेशा उस memory block के size के हिसाब से move होता है, जिसका वह pointer है।
| Data Type | Size (in bytes) |
|---|---|
char | 1 byte |
int | 4 byte |
float | 4 byte |
double | 8 byte |
मतलब:
- अगर
ptrएकintpointer है →ptr++करने पर address 4 bytes आगे बढ़ेगा। - अगर
ptrएकcharpointer है →ptr++करने पर address 1 byte आगे बढ़ेगा।
Pointer Increment और Decrement
Example:
#include <stdio.h>
int main()
{
int a = 10;
int *ptr = &a;
printf("Old Address : %d\n", ptr);
ptr++;
printf("New Address : %d\n", ptr);
return 0;
}
Output:
Old Address : 6356728
New Address : 6356732
Explanation:
int का size = 4 bytes
तो ptr++ करने पर pointer 4 bytes आगे शिफ्ट हो गया।
Pointer Addition
Syntax:
ptr = ptr + n;
Calculation (Internal):
new_address = current_address + (n * sizeof(data_type));
Example:
int a = 10;
int *ptr = &a;
int *ntr = ptr + 2;
यहाँ ntr pointer, ptr से दो int locations आगे चला जाएगा।
यानी address shift होगा → 2 * 4 = 8 bytes
Pointer Subtraction
Syntax:
ptr = ptr - n;
Internal:
new_address = current_address - (n * sizeof(data_type));
Example:
char ch = 'A';
char *ptr = &ch;
char *ntr = ptr - 1;
char का size 1 byte है
तो यहाँ address 1 byte पीछे जाएगा।
Pointer Increment & Decrement Example
#include <stdio.h>
int main()
{
int a = 5;
int *ptr = &a;
printf("ptr : %d\n", ptr);
ptr++;
printf("ptr++ : %d\n", ptr);
ptr--;
printf("ptr-- : %d\n", ptr);
return 0;
}
Pointer Comparison
Pointer comparison में हम यह देखते हैं कि क्या दो pointers same address को point कर रहे हैं या नहीं।
| Operator | Meaning |
|---|---|
== | दोनों pointers एक ही address को point कर रहे हों |
!= | दोनों pointers बराबर न हों |
Example:
#include <stdio.h>
int main()
{
int x = 5;
int y = 10;
int *p1 = &x;
int *p2 = &x;
int *p3 = &y;
if(p1 == p2)
printf("p1 and p2 same location point kar rahe hain\n");
if(p1 != p3)
printf("p1 and p3 different location point kar rahe hain\n");
return 0;
}
Same Array के Elements पर Pointer Comparison
अगर Pointer किसी array के elements को represent कर रहा है, तब <, > comparison meaningful होता है।
Example:
#include <stdio.h>
int main()
{
int arr[5] = {34, 54, 65, 37, 21};
int *ptr = arr;
printf("Array Elements : ");
for(; ptr <= &arr[4]; ptr++)
{
printf("%d ", *ptr);
}
return 0;
}
Output:
Array Elements : 34 54 65 37 21
Important Points (Short Notes)
| Point | Explanation |
|---|---|
| Pointer arithmetic data type के size पर depend करती है | ptr++ का मतलब हमेशा +1 byte नहीं होता |
| दो pointers का addition possible नहीं है | लेकिन subtraction possible है |
| Pointer increment का मतलब value बढ़ना नहीं, address move होना है | ptr++ pointer को अगले memory block पर ले जाता है |
Comparison ज़्यादातर == और != के लिए होता है | खासकर यह check करने के लिए कि pointer खाली है या नहीं |
Conclusion
इस tutorial में हमने जाना कि Pointer arithmetic C language का एक powerful part है, जो memory को efficiently control करने में मदद करता है। Pointer arithmetic तभी useful होता है जब आपको memory के अंदर सीधे काम करना हो, जैसे arrays, dynamic memory allocation, data structures, आदि में।
Next Tutorial
अगले tutorial में हम सीखेंगे:
✔ Dynamic Memory Allocation (malloc, calloc, free) का इस्तेमाल
✔ RAM में memory को runtime पर allocate करना



