pointer एक derived data type है। जिसका उपयोग variable के address को access करने और उन्हें manipulate करने के लिए किया जाता है।
इसे विस्तार से समझते है।
C ++ हो या अन्य programming language , हम सबसे पहले program में, कोई task के लिए variable declaration करते हैं जिसमें user द्वारा दिए गए input या information store होते हैं। variable memory में इन values को store करता है।
अब जैसे कि प्रत्येक information को byte के रूप में store किया जाता है और इन bytes का अपना एक address होता है, इसलिए इसका अर्थ है कि memory में stored values का अपना एक address होता है।
अब, हम C ++ में pointer -variable का उपयोग करके इन values के address को access करते हैं।
तो definition है,
pointer एक प्रकार का variable होता है, जो value / variable के address को store करता है। C ++ में Pointer वेरिएबल का उपयोग करके हम किसी भी variable के address को access कर सकते हैं और उन्हें manipulate कर सकते हैं।
declaration of a pointer variable
pointer -variable एक सामान्य variable की तरह होता है, लेकिन जब हम इसमें asterisk (*) जोड़ते हैं, तो यह एक pointer variable बन जाता है।
इसका syntax नीचे दिया गया है –
SYNTAX
data-type *variable-name;
यहां, data type किसी भी प्रकार का data हो सकता है जैसे- int , char , float
और void
asterisk symbol को data -type और variable-name के बीच कहीं भी declare किया जा सकता है, जैसे कि
Data-type* variable-name;
ध्यान दें , यहाँ पर वही data type होना चाहिए जिस data -type का आप address store करना चाहते हैं। अर्थात , हम float
data type के साथ int
type pointer-variable प्रयोग नहीं कर सकते हैं जैसे –
int x; float *y;
y = &x; // wrong
int x,*y;
y = &x; // right
access variable address using Pointer variable in C++
एक variable के address को access करने के लिए, हम pointer में 2 प्रकार के operators का प्रयोग करते हैं-
- value of Operator (*)
- address of Operator (&)
value of Operator
इसका उपयोग address में stored value को print करने के लिए किया जाता है। इसे de-reference operator भी कहा जाता है।
int *ptr;
int x = 6;
cout<<*ptr1; // 6
address of operator
इसका उपयोग Value के Address को print करने के लिए किया जाता है। इसे value of operator भी कहा जाता है जैसे-
int *ptr, x = 6;
ptr = &x;
cout<<ptr; // address will be print
de -reference operator (*) का उपयोग किए बिना भी , हम एक variable के address को print कर सकते हैं, यानी किसी variable के address को केवल & operator के इस्तेमाल किये बिना भी print किया जा सकता है।
cout<<&x; // x variable address will be print
परन्तु इससे केवल address को print ही किया जा सकता है उसे store या manipulate (जो की pointer का main feature है ) नहीं किया जा सकता।
& operator का उपयोग करके, हम खुद pointer -variable के address को भी access कर सकते हैं जैसे –
cout<<&ptr; // pointer-variable address will be print
अब आप कह सकते हैं कि हम pointer variable का उपयोग उस variable के address को store करने के लिए करते हैं जिसे अन्य साधारण variable store नहीं कर सकते।
यहाँ निचे इसका program दिया गया है –
access variable using Pointer variable
#include<iostream.h>
#include<conio.h>
void main()
{
int x = 6,*ptr;
ptr = &x;
clrscr();
cout<<"value store the address: "<<*ptr<<endl;
cout<<"Addres of this variable: "<<ptr<<endl;
cout<<"\nanother way using &x : "<<&x<<endl;
cout<<"Addres of pointer variable: "<<&ptr<<endl;
getch();
}
OUTPUT
value store the address: 6
Addres of this variable: 0x8f29fff2
another way using &x : 0x8f29fff2
Addres of pointer variable: 0x8f29fff0
✍ pointer का उपयोग एक variable के address को access करने के लिए किया जाता है, साथ ही dynamic memory allocation के लिए भी।
Pointer C ++ का एक powerful feature है जिससे हम memory को access करते है। पर क्योंकि इसका सबंध सीधे memory से होता है जिसे values के memory address को manipulate किया जा सकता है इसलिए यह कभी कभी हानिकारक भी हो सकता है क्योंकि यह secure नहीं होता। इसलिए यह feature modern languages में नहीं पाया जाता।
more about pointer