• Introduction to C++ pointers in Hindi
  • Declaring & initializing C++ pointers in Hindi
  • C++ Pointer to pointer in Hindi

Introduction to C++ Pointers

Pointers के बारे में आप C language में पहले पढ़ चुके है। C++ में भी pointers को उसी प्रकार यूज़ किया जाता है। Pointers variables में आप किसी normal variable का address store करते है।

इस address के द्वारा pointer variables उस original variable की value को point करते है। Pointers कभी भी value को store नहीं करते है बल्कि pointer variables address के माध्यम से उस value को point करते है।

आप सड़क पर खड़े थे किसी ने आप से आकर पूछा भाई साहब रमेश कँहा रहता है। आपने रमेश के घर की तरफ इशारा कर दिया। इसी प्रकार pointer variables भी सिर्फ इशारा करते है की particular value किस location पर stored है।    

Pointers के द्वारा आप memory में store की गयी value को program में कई जगह से point कर सकते है। Pointers के द्वारा variables की values के साथ operations भी perform किये जा सकते है जैसे की variables को print करना आदि।

हालाँकि आप values को variable names से access कर सकते है, फिर भी आपको pointers के रूप में एक alternate approach provide की गयी है। आइये इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।

मान लीजिये आपने एक int type का Age variable create किया है।

int age; 

जब आप इस variable को किसी value के साथ initialize करेंगे तो compiler memory में variable के नाम से एक memory location allot करेगा।

int age = 60; 

ऊपर दिए गए उदाहरण में Age उस memory location का नाम है और 60 उस memory location में store की गयी value है। इस memory location का एक address भी होगा जो की hexadecimal form में होता है। Pointers इसी address को store करते है और इस address के माध्यम से ही वे original variable की value को point करते है।
एक pointer से आप किसी दूसरे pointer variable को भी point कर सकते है। C++ में arrays के भी pointers create कर सकते है। Objects के लिए pointers create करने की ability भी C++ आपको provide करती है। Pointers को functions के साथ भी यूज़ किया जाता है।

आइये अब देखते है की C++ में आप किस प्रकार pointers create और use कर सकते है।

Declaring & Initializing C++ Pointers

Pointer variables create करने के लिए आप asterisk (*) operator को use करते है। जिस type का original variable होता है उसी type का pointer variable create किया जाता है। C++ में आप primitive types से लेकर user defined type तक के pointer variables create कर सकते है। 

data type  *pointer-variable-name; 
            
जैसा की आप ऊपर दिए गए syntax में देख सकते है, सबसे पहले data type define किया गया। इसके बाद asterisk (*) variable के साथ pointer variable का नाम declare किया गया है।  

// Normal variable
int age = 5;  

// Pointer variable
int *pntr;     

Pointer variables को आप address of (&) operator के साथ initialize करते है। 

pointer-variable = & original-variable;  

pntr = &age;  

ऊपर दिए गए उदाहरण में age variable का address ptr pointer variable को assign किया गया है।  

Example

#include <iostream>
using namespace std;

int main()
{
     // Regular variable 
     int age = 50;

     // Pointer variable 
     int *ptr;
 
     // Assigning normal variable to pointer 
     ptr = &age;
     
     cout<<"Value of age variable using normal syntax : "<<age<<endl;
     cout<<"Address of age variable : "<<ptr<<endl;
     cout<<"Value of age variable using pointer variable : "<<*ptr<<endl;

ऊपर दिए गए उदाहरण में value at (*) operator के साथ pointer variable को यूज़ किया गया है। ये operator किसी address पर store की गयी value को access करने के लिए यूज़ किया जाता है। ऊपर दिया गया program निचे दिया गया output generate करता है।

Value of age variable using normal syntax : 50
Address of age variable  : 0xbff28fc0
Value of age variable using pointer variable : 50

Pointer to Pointer 

जैसा की आपको पहले बताया गया एक pointer से दूसरे pointer variable को भी point किया जा सकता है। इसके लिए double asterisk (**) operator को यूज़ किया जाता है। इसका syntax नीचे दिया जा रहा है। 

data type **pointer-name;

मान लीजिये आप पहले ऊपर create किये गए ptr variable के लिए एक pointer create करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।  

int **ppntr; 

इस pointer variable को आप इस प्रकार initialize करेंगे।

ppntr = ptr; 

Pointer to Array 

Pointer variables के द्वारा आप array को भी point कर सकते है। मान लीजिये आपने एक num नाम से integer array create किया है। इसमें आपने values भी store की है।

int num[5] = {1,2,3,4,5}; 

इस array के लिए आप pointer इस प्रकार create कर सकते है।

int *arrpntr; 

इस pointer को इस प्रकार initialize किया जायेगा।

arrpntr = num; 

arrpntr इस समय num array के first element का address hold कर रहा है। आप चाहे तो arrpntr variable को loop के द्वारा increment करते हुए value at (*) operator के द्वारा आप array के सभी elements को print भी कर सकते है।

for(int i =0; i<=5; i++)
{
       cout<<"Value at first<<i<<"is :"<<arrpntr+i;

Pointer to Objects

जैसा की आपको पहले बताया गया आप user defined types (class objects) के लिए भी pointers create कर सकते है। किसी भी class type का pointer create करके आप उससे किसी normal object को point कर सकते है। इस pointer के द्वारा आप class के members को access कर सकते है। आइये इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।

class item
{
      private:
               int id = 5;
               int price = 1000;
      public:
              void display(void)
             {
                    cout<<"Item id is "<<id<<" and price is :"<<price;
             }
}; 

int main()
{
    item obj;

    // Pointer to object
    item *obpntr;

    // Pointing to object using object pointer
    obpntr = &obj;
 
    // Calling function with object pointer
    obpntr->display();
}
    
ऊपर दिए गए उदाहरण में item नाम से एक class create की गयी है। Main function में इस class का एक object और एक pointer variable create किया गया है। इसके बाद object के address को address of (&) operator द्वारा pointer variable को assign किया गया है।

इसके बाद pointer variable के माध्यम से class के display() function को execute करवाया गया है। ये program निचे दिया गया output generate करता है।

Item id is 5 and price is 1000 

Pointer to Function

आप functions के लिए भी pointers create कर सकते है और उन्हें execute कर सकते है। इसका general syntax नीचे दिया जा रहा है। 

data-type (*pointer-name) (parameters); 

मान लीजिये आपने एक addition का function create किया हुआ है। 

int add(int a, int b)
{
     return a+b;
   
ऊपर दिए function के लिए pointer इस प्रकार create किया जायेगा। 

int (*funpntr) (int, int); 

इस statement के द्वारा एक function pointer type create किया गया है। इस type का pointer variable create करके आप उसे add function का address assign करते है। इसका उदाहरण नीचे दिया जा रहा है।

int main()
{
    int (*funpntr) (int,int); 
    funpntr = &add;

   cout<<"Addition of 2 & 3 is "<<funpntr(2,3);

   return 0;

इसे एक complete उदाहरण के रूप में निचे दिया जा रहा है।

#include <iostream>
using namespace std;

int add(int a, int b)
{
     return a+b;

int main()
{
    // Declaring function pointer
    int (*funpntr) (int,int);

    // Assigning function to pointer 
    funpntr = &add;

    cout<<"Addition of 2 & 3 is : "<<funpntr(2,3);

    return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Addition of 2 & 3 is : 5 

This pointer 

This एक pointer होता है जो हर class के साथ automatically attached होता है। इस pointer को current class के सन्दर्भ में यूज़ किया जाता है। इसे सिर्फ class के अंदर ही यूज़ किया जा सकता है।

मान लीजिये आप किसी reason से class के किसी function को class में ही execute करवाना चाहते है। लेकिन object के बिना class के किसी भी function को execute करना possible नहीं है। ऐसी situation में आप this pointer यूज़ कर सकते है और function को execute करवा सकते है।

#include <iostream>
using namespace std;

class thisdemo
{
    public:
             void byebye(void)
             {
                  cout<<"Bye Bye! Take care!";
             }
             
             void welcome(void) 
             {
                        cout<<"Welcome into this program. How are you feeling in here..."<<endl;

                        // Calling byebye() function using this pointer
                        this->byebye();   
             }
}; 

int main()
{
        thisdemo obj;
        obj.welcome();

       return 0;
}
         
ऊपर दिए गए उदाहरण में जैसा की आप देख सकते है thisdemo class के object से सिर्फ welcome() function को call किया गया है। Welcome function में this pointer द्वारा byebye() function को call किया गया है। जब आप welcome() function को call करते है तो byebye() function भी call हो जाता है।

जब भी आपको बिना object के class में members को access करने की आवश्यकता हो तो आप this pointer को यूज़ करेंगे।

Welcome into this program. How are you feeling in here... 
Bye Bye! Take care!