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 ही होता है, फर्क सिर्फ इतना है कि:

सामान्य VariablePointer 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 कर सकते हैं:

OperationMeaning
ptr++Pointer को अगले memory location पर ले जाए
ptr--Pointer को पिछले memory location पर ले जाए
ptr + nPointer में n locations आगे जाए
ptr - nPointer में n locations पीछे जाए
ptr1 == ptr2Compare करें कि क्या दोनों same memory address को point करते हैं
See also  C Programming में Functions का use कैसे करते है? - Functions in C Programming in Hindi

Important Rule

Pointer arithmetic करते समय data type बहुत महत्वपूर्ण है।

क्योंकि Pointer हमेशा उस memory block के size के हिसाब से move होता है, जिसका वह pointer है।

Data TypeSize (in bytes)
char1 byte
int4 byte
float4 byte
double8 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 कर रहे हैं या नहीं।

OperatorMeaning
==दोनों pointers एक ही address को point कर रहे हों
!=दोनों pointers बराबर न हों
See also  C Programming में nested if else conditional statement का use कैसे करते हैं? - Nested IF Else Statement in C Programming in Hindi

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)

PointExplanation
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 करना

See also  Python Programming क्या होता है? Python Programming सीखने से क्या फायदा है? - Python Tutorial in Hindi

HTN

Hindi Tech News is a trusted technology website dedicated to delivering the latest tech updates in simple and easy Hindi. Our goal is to make technology understandable for everyone — beginners, students, tech lovers, and digital learners.

Related Articles

Back to top button