What is pointer in C++ in hindi || पॉइंटर किसे कहते हैं?

0 Deepak
What is pointer in C++ in Hindi. Write the procedure to define pointer and C++.


Table of Content (toc)


परिचय 

पोइंटर C++ language का  एक मुख्य feature है। पोइंटर एक स्पेशल variable है जिसका उपयोग किसी अन्य variable के memory address को स्टोर करने के लिए किया जाता है। जिस प्रकार एक साधारण variable को डिक्लैर किया जाता है उसी प्रकार पोइंटर को भी डिक्लैर किया जाता है। 

Pointer in C++ in Hindi
Pointer in C++ in Hindi


What is pointer in C++ in hindi

Pointer किसे कहते हैं?(Definition)

वह variable जो अन्य variable का address स्टोर करता है, पोइंटर कहलाता है। 


Declaration

किसी अन्य variable की तरह पोइंटर को भी इस्तेमाल करने से पहले डिक्लैर करने की आवश्यकता होती है। पोइंटर के variable को डिक्लैर करने के लिए (*) चिन्ह का प्रयोग किया जाता है, इसे पोइंटर ऑपरेशन कहते हैं। 

 data_type * pointer_name  

program code of Pointer

 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
  int number[50];*ptr  
  int n,i;  
  cout<<"\n Enter the count \n;  
  cin>>n;  
  cout<<"\n Enter the number one by one;  
  for(i=0; i<n; i++)  
 {  
  cin>>numbers[i];  
  }  
 ptr = numbers;  
 int sum = 0;  
 for (i=0; i<n; i++)  
 {  
  if (*ptr %2 == 0)  
 sum = sum+ **ptr;  
 ptr++;  
 }  
 cout<<"\n\n sum of even numbers ="<<sum;  
 }  

Output

 Enter the count  
 5  
 Enter the number one by one   
 10  
 16  
 25  
 45  
 34  
 sum of even numbers = 60  

    Uses of Pointer in Hindi

    1. सभी function में एक से अधिक value return करना। 
    2. मेमोरी elements को एक्सैस करने में।
    3. मेमोरी व डाटा टेबल को संभालने में pointers अधिक सक्षम होते हैं। 
    4. Pointers प्रोग्राम के समय की अवधि व जटिलता को कम करते हैं। 
    5. किसी variable के मेमोरी एड्रैस को एक्सैस करने में। 
    6. डाइनैमिक मेमोरी में array तथा string को pass करने में। 
    7. किसी function में array तथा string को pass करने में। 
    8. low level programming करने में। 

    Pointer to function

    function variable के समान होता है जिसका एक मेमोरी लोकेशन होता है। function पोइंटर, C++ की दूसरी महत्वपूर्ण विशेषता है। जिस प्रकार interger, character तथा floats के पास मेमोरी में address हैं उसी प्रकार function के पास भी मेमोरी में physical address होता है। यह एड्रैस function का प्रवेश बिन्दु है जिसे पोइंटर पर assign किया जाता है और पोइंटर का प्रयोग function को invoke करने के लिए किया जा सकता है। 

    Program code

     #include <iostream>
    #include <conio.h>
    using namespace std;

    class A {
    private:
        void swap(int* p, int* a) {
            int r;
            r = *p;
            *p = *a;
            *a = r;
        }

        // To allow access from main, we can either:
        // (a) make 'swap' public, or
        // (b) create a public wrapper function
    public:
        void do_swap(int* p, int* a) {
            swap(p, a);
        }
    };

    void main() {
        int x, y;
        cout << "Enter two numbers" << endl;
        cin >> x >> y;

        A ob;
        ob.do_swap(&x, &y); // Call public wrapper

        cout << "After swapping:" << endl;
        cout << "x = " << x << ", y = " << y << endl;

        getch();
    }(code-box)

    Sample Output

    Enter two numbers
    5 10
    After swapping:
    x = 10, y = 5(code-box)

    Pointer to Pointer

    Array में flexibility लाने और पोइंटर का इस्तेमाल function में करने के लिये pointer to pointer का इस्तेमाल किया जाता है। 

    Syntax 

    data_type** ptr;(code-box)

    उदाहरण

    #include <iostream>
    #include <conio.h>
    using namespace std;

    void main() {
        int* iptr; // Pointer to int
        int** ptriptr; // Pointer to pointer to int
        int data;

        iptr = &data; // iptr points to data
        ptriptr = &iptr; // ptriptr points to iptr

        *iptr = 100; // data = 100
        cout << "The variable data contains : " << data << endl;

        **ptriptr = 200; // data = 200
        data = 300;

        cout << "ptriptr is pointing to = " << *iptr << endl;

        getch();
    }(code-box)

    Output

    The variable data contains : 100

    ptriptr is pointing to = 300(code-box)

    Pointer to Array

    जिस प्रकार एक सामान्य data_type जैसे integer, float, character आदि से array बनाए जा सकते हैं। ठीक उसी प्रकार एक पोइंटर के साथ भी array को डिक्लैर किया जाता है ठीक उसी प्रकार एक int array, int value का group होता है। वैसे ही पोइंटर का array बहुत से मेमोरी एड्रैस का ही group होता है। pointers के array में मेमोरी एड्रैस अलग अलग variable के सतह हो सकते हैं। 

    Syntax 

    <data_type>* pointer_name[size];(code-box)

    उदाहरण 

    #include <iostream>
    #include <conio.h>
    using namespace std;

    void main() {
        static int a[4] = {1, 2, 3, 4}; // Corrected declaration
        int i, n, temp;
        n = 4;

        cout << "Contents of the array" << endl;

        for (i = 0; i < n; i++) {
            temp = *(&(a[0]) + i); // Correct pointer expression
            cout << "value = " << temp << endl;
        }

        getch();
    }(code-box)

    Output

    Contents of the array
    value = 1
    value = 2
    value = 3
    value = 4(code-box)

    Pointer to Object

    जिस तरह variable के अन्य प्रकारों के pointers होते हैं उसी प्रकार हम Object के pointer भी बना सकते हैं, Pointers किसी class के द्वारा बनाए गए  objects को निर्दिष्ट करता है। object के pointers किसी class के object को निर्दिष्ट करने वाले विशेष pointers होते हैं। 

    Syntax

    class_name* pointer_name;(code-box)

    उदाहरण

    #include <iostream>
    #include <conio.h>
    using namespace std;

    class Product {
        int code;
        float price;

    public:
        void get_data(int a, float b) {
            code = a;
            price = b;
        }

        void show() {
            cout << "Code: " << code << endl;
            cout << "Price: " << price << endl;
        }
    };

    void main() {
        clrscr();

        Product x;
        Product* ptr = &x;

        ptr->get_data(100, 60.50);
        ptr->show();

        getch();
    }(code-box)

    इस लेख हम आपको what is printer in C++ in hindi || (pointer किसे कहते हैं?) इसकी declarartion और इससे जुड़ी प्रोग्रामिंग से परिचित करने की कोशिश की गयी है।

    आशा करता हूँ की आपको pointer से संबंधित ये सब जानकारी आपको समझ आया होगा।

    लेख पसंद आया तो इसे जरूर share करें।

    Post a Comment

    0 Comments
    * Please Don't Spam Here. All the Comments are Reviewed by Admin.

    #buttons=(Ok, Go it!) #days=(20)

    Our website uses cookies to enhance your experience. Check Out
    Ok, Go it!