Pointer Arithmetic in C Programming in Hindi

इस tutorial में हम सीखेंगे की कैसे आप C programming में pointer arithmetic operations जैसे की addition, subtraction, pointer increment, decrement और comparison कर सकते हो.

जैसा की हमने इससे पहले वाले tutorial में पढ़ा था की pointer भी variable ही होते है जो किसी memory address (location) को points करते हैं.

C programming में आप pointer के साथ normal variable की तरह arithmetic operations भी कर सकते हैं लेकिन आप pointer के साथ सिर्फ नीचे दिए गए operations ही कर सकते हो.

  • Increment and decrement (++ and –)
  • Addition and Subtraction (+ and -)
  • Comparison (<, >, <=, >=, ==, !=)

Pointer variable के साथ increment, decrement, addition या subtraction के operators का use करने से पहले हम normal variable के साथ इन operators का use नीचे दिए गए example के साथ समझेंगे.

#include<stdio.h>
int main()
{
    int a,b;

    a=5;
    b=a+1;
    
    printf("Value of A : %d\n",a);
    printf("Value of A : %d\n",b);

    a=a+1;

    printf("Value of A : %d\n",a);

    a++;

    printf("Value of A : %d\n",a);

    return 0;
}

Output:

Value of A : 5
Value of B : 6
Value of A : 6
Value of A : 7

Explanation:

ऊपर दिया हुआ program बहुत आसान है. आपको बस इतना समझना है की a+1 और a=a+1 expression में अंतर है. अगर आप सिर्फ a+1 या b=a+1 लिखेंगे तो इसमें a की value change (update) नहीं होगी. 

इसके अलावा a++ और a=a+1 expression का मतलब एक ही है इसलिए जब आप a++ या a=a+1 लिखेंगे तो इसमें a की value change हो जाएगी.

ऊपर program में line 7 पर जब हमने b=a+1 लिखा तब वहां सिर्फ a की value 5 में 1 add होगा ना की a में और फिर 5 में 1 add होने के बाद value 6 variable b में assign हो जाएगा.

इसी तरह जब pointer variable ptr को सिर्फ ptr+1 लिखेंगे तो इससे pointer की अपनी value change नहीं होगी लेकिन जब आप ptr++ लिखेंगे तो इससे pointer की value में change हो जाएगी.

Pointer Addition and Subtraction in C

Pointer addition की सबसे जरूरी बात जो है वो ये है की आप pointer variable के साथ सिर्फ integer value को add कर सकते हो किसी और data type की value को नहीं. इसके अलावा आप 2 pointers को भी आपस में add नहीं कर सकते हो.

Pointer addition और pointer subtraction का syntax और work process लगभग सब एक जैसा ही है.

Pointer Addition Syntax:

pointer_variable + number;

Pointer Addition Internally Syntax:

new_address = current_address + (number * size_of(datatype));

Pointer Subtraction Syntax:

pointer_variable - number;

Pointer Subtraction Internally Syntax:

new_address = current_address - (number * size_of(datatype));

आप अपने program में पहले syntax के हिसाब से pointer addition या subtraction expression लिखते हैं लेकिन compiler खुद से आपके expression को internally syntax के हिसाब से बदल कर solve करके new address return करता है.

Pointer Addition Example Program 1:

#include<stdio.h>
int main()
{
    int a,*ptr;

    ptr=&a;

    printf("Value of ptr : %d\n",ptr);

    printf("Value of ptr+1 : %d",ptr+1);

    return 0;
}

Output:

Value of ptr : 6356728
Value of ptr+1 : 6356732

Explanation:

ऊपर output के हिसाब से variable a का address मेरे computer में 6356728 था जो हमने pointer ptr को assign करके पहले printf() function से print करा दिया.

दूसरे printf() function में जब हमने pointer ptr के साथ value 1 को add किया तब वहां internally जो change हुआ होगा वो आप नीचे देख सकते हो.

printf("Value of ptr+1 : %d", ptr + ( 1 * sizeof(int) ) );

आप जिस data type के pointer के साथ addition करेंगे sizeof() function उसी data type का size find करेगा. मेरे computer में int data type 4 bytes space occupied करता है.

Data type का size find होने के बाद आप जो number pointer के साथ add कर रहे है उसका data type के size के साथ multiply होगा और उसके result को pointer variable के current address के साथ add करके new address print हो जाएगा.

ऊपर program में हमने pointer addition result यानी new address को direct print करा दिया है आप चाहो तो उस address को किसी variable में store भी कर सकते थे. जैसा की हमने नीचे दिए गए program में किया है.

Pointer Addition Example Program 2:

#include<stdio.h>
int main()
{
    int a,*ptr,*ntr;

    ptr=&a;
    ntr=ptr+2;

    printf("Value of ptr : %d\n",ptr);
    printf("Value of ntr : %d\n",ntr);

    ptr=ptr+1;

    printf("Value of ptr : %d\n",ptr);

    return 0;
}

Output:

Value of ptr : 6356724
Value of ntr : 6356732
Value of ptr : 6356728

Explanation:

ऊपर output के हिसाब से variable a का address मेरे computer में 6356724 था जो हमने pointer ptr को assign कर दिया था.

उसके बाद जब हमने line 7 पर pointer ptr में value 2 को add करके new address (6356732) को pointer ntr में assign किया है यानी pointer ptr अभी भी पुराने address (6356724) को ही point करेगा.

इसके बाद line 12 पर हमने pointer ptr में value 1 को add करके new address को pointer ptr में ही assign किया है यानी pointer ptr अब new address (6356728) को point करेगा.

Pointer Subtraction Example Program:

#include<stdio.h>
int main()
{
    char ch,*ptr,*ntr;

    ptr=&ch;
    ntr=ptr-1;

    printf("Value of ptr : %d\n",ptr);
    printf("Value of ntr : %d\n",ntr);


    return 0;
}

Output:

Value of ptr : 6356727
Value of ntr : 6356726

Explanation:

ऊपर output के हिसाब से variable ch का address मेरे computer में 6356727 था जो हमने pointer ptr को assign कर दिया था.

उसके बाद जब हमने line 7 पर pointer ptr में value 1 को subtract करके new address (6356726) को pointer ntr में assign किया है.

मेरे computer में char data type का pointer 1 byte space occupied करता है इसलिए pointer ptr के address में value 1 subtract करने पर सिर्फ 1 byte की subtract हुई.

Pointer Increment and Decrement in C

Pointer increment और decrement operations लगभग pointer addition और subtract operations की तरह ही होता है. 

इसलिए pointer increment और decrement operations समझने में आपको ज्यादा problem नहीं आएगी.

Pointer increment and decrement syntax:

ptr++;

ptr--;

आपको बस इतना समझना है की ptr+1 और ptr=ptr+1 expression में अंतर है. अगर आप सिर्फ ptr+1 या ntr=ptr+1 लिखेंगे तो इससे ptr की value जोकि एक address है वो change नहीं होगा.

इसके अलावा ptr++ और ptr=ptr+1 expression का मतलब एक ही है इसलिए जब आप ptr++ या ptr=ptr+1 लिखेंगे तो इससे ptr की value change हो जाएगी यानी ptr अब नए address को point करेगा.

Pointer addition और pointer increment की तरह ही pointer subtraction और pointer decrement काम करता है.

Pointer arithmetic operations को और ज्यादा अच्छे से समझने के लिए और नीचे दिए गए pointer comparison topic को समझने के लिए पहले आप नीचे दिए गए link पर click करके program को जरूर समझें.

Read: Pointer with Array in C Programming in Hindi

Pointer Comparison in C

जैसे हम relational operators ( <, <=, >, >= , == , !=) का use करके normal variable का comparison कर सकते हैं ठीक ऐसे ही हम relational operators की help से आप pointer varaible का comparison कर सकते हैं.

Pointer comparsions में ज्यादातर == और != operators का use होता है. इन दोनों operators की help से हम ये compare करते हैं की दो pointers same address को point कर रहे हैं या नहीं.

दो pointers को equal तब माना जाता है जब दोनों किसी address को point नहीं कर रहे हो यानी दोनों की value null हो या फिर दोनों same address को point कर रहे हो.

Equal to ( == ) और Not Equal to ( != ) operators के अलावा other relational operators (<, <=, >, >=) का use ज्यादातर उन दो pointers के साथ किया जाता है जो same array के elements के addresses को point कर रहे हो.

Pointer comparison example program 1:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int *ptr,*ntr;

    ptr=(int *)malloc(sizeof(int));
    ntr=(int *)malloc(sizeof(int));

    if(ptr==ntr)
    {
        printf("Both pointers have same address");
    }
    else
    {
        printf("Both pointers have different addresses");
    }

    return 0;
}

Output:

Both pointers have different addresses

Pointer comparison example program 2:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int arr[]={34,54,65,37,21};
    int i,*ptr;

    ptr=&arr[0];

    printf("Array Elements : ");
    for(i=1;ptr<=&arr[4];i++)
    {
        printf("%d ",*ptr);
        ptr++;
    }

    return 0;
}

Ouput:

Array Elements: 34 54 65 37 21

What’s Next: इस tutorial में हमने Pointers Arithmetic के बारे में पढ़ा. Next tutorial में हम C programming में Dynamic Memory Allocation का use करना सीखेंगे