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 |
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
- सभी function में एक से अधिक value return करना।
- मेमोरी elements को एक्सैस करने में।
- मेमोरी व डाटा टेबल को संभालने में pointers अधिक सक्षम होते हैं।
- Pointers प्रोग्राम के समय की अवधि व जटिलता को कम करते हैं।
- किसी variable के मेमोरी एड्रैस को एक्सैस करने में।
- डाइनैमिक मेमोरी में array तथा string को pass करने में।
- किसी function में array तथा string को pass करने में।
- 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 functionpublic: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 wrappercout << "After swapping:" << endl;cout << "x = " << x << ", y = " << y << endl;getch();}(code-box)
Sample Output
Enter two numbers5 10After 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 intint** ptriptr; // Pointer to pointer to intint data;iptr = &data; // iptr points to dataptriptr = &iptr; // ptriptr points to iptr*iptr = 100; // data = 100cout << "The variable data contains : " << data << endl;**ptriptr = 200; // data = 200data = 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 declarationint i, n, temp;n = 4;cout << "Contents of the array" << endl;for (i = 0; i < n; i++) {temp = *(&(a[0]) + i); // Correct pointer expressioncout << "value = " << temp << endl;}getch();}(code-box)
Output
Contents of the arrayvalue = 1value = 2value = 3value = 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 करें।
Hi please, do not spam in comments