हिंदी माध्यम नोट्स
Address variable in c++ language , Address variable within Function , Reference variable in hindi
c++ मे नए compund variable को add किया गया है | जिसे address variable कहते है | reference का नाम जो की alias की तरह कार्य करता है | उदहारण के लिए variable name का reference n है अतः name और n दोनों को ही variable use किया जा सकता है | जब किसी reference को argument की तरह कार्य करता है तब function orginal data की जगह इसकी copy को use किया जाता है | reference variable को pointer variable की जगह use किया जाता है |
Creating Reference variable
C or C++ languagr मे “&” operator को किसी operator के address को define किया जाता है | c++ language मे किसी variable को ‘&’ से declare किया जाता है | तब इसे address variable कहते है | इस variable को address को define करने के लिए किया जाता जाता है | उदाहरन के लिए ‘a’ एक variable है और name एक address variable है |
int a =10 ;
int &name = a;
इस उदाहरन मे ‘&’ का variable के address को define नहीं करा रहा है | बल्कि ये address variable को define कर रहा है | जैसे char * : इस type से character type के pointer को define करा रहा है | इसी तरह int & : इस type से integer type के address variable को define करता है | reference declartion name और a को interechange करने की facility provide करवाता है | लेकिन दोनों variable एक ही memory address और value को contain करता है |
उदहारण के लिए :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
using namespace std;
int n=101;
int &number= n;
cout<<“value of n : “<<n<<endl;
cout<<“value of number : “<<number<<endl;
number++;
cout<<“value of n : “<<n<<endl;
cout<<“value of number : “<<number<<endl;
cout<<“Address of n :”<<&n;
cout<<“Address of number :”<<&number;
getch();
}
इस उदाहरन मे number address variable नहीं है | लेकिन number variable , integer type के address को define करता है | इसके अलावा cout<<“Address of n :”<<&n; और cout<<“Address of number :”<<&number; मे use किये गया &n और &number address variable है | और n और number की value और address सामान होती है | जब number की value को increment किया जाता है | दोनों variable की value बढती है | C language मे , address variable को use करना थोडा confuse होता है लेकिन c++ language मे इसे pointer variable को use किया जाता है | इसके लिए उदहारण निन्म है |
int n =10 ;
int &name = a;
int *num=&n;
इस उदाहरन मे , num और n दोनों एक ही variable है | लेकिन name = n को use नहीं करा सकते है | लेकिन इसके लिए निन्म उदाहरन है :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
using namespace std;
int n=101;
int &number= n;
cout<<“value of n : “<<n<<endl;
cout<<“value of number : “<<number<<endl;
int second = 12 ;
number = second ;
cout<<“value of second : “<<second<<endl;
cout<<“value of n : “<<n<<endl;
cout<<“value of number : “<<number<<endl;
cout<<“Address of second :”<<&n;
cout<<“Address of number :”<<&number;
getch();
}
इस उदहारण मे , number = second ; execute होता है | इसके second की value को number मे assign हो जाता है | लेकिन second और number का address value अलग अलग होता है | लेकिन number और second का address सामान है |
Address variable within Function
reference को function मे parameter की तरह use किया जाता है | जब किसी variable नाम को किसी variable से call किया जाता है |जब किसी reference को function मे pass किया जाता है इसे passing by reference कहते है | passing by reference method मे , called function से variable को calling function से access किया जाता है | इस method मे variable के copy को use किया जाता है | function को तीन method से call किया जाता है |
call by value : value को function मे pass किया जाता है |
call by address : address और pointer variable को function मे pass किया जाता है |
call by reference : reference variable को function मे pass किया जाता है |
इसका उदाहरन निन्म है :
#include<iostream.h>
#include<conio.h>
void swapreference(int & a , int & b );
void swappointer(int *p , int *q);
void swapvalue(int a , int b );
void main()
{
int value1 = 100;
int value2 = 200;
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
cout<<“Swap with reference :”<<endl;
swapreference(value1 , value2 );
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
cout<<“Swap with pointer :”<<endl;
swappointer(value1 , value2 );
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
cout<<“Swap with value :”<<endl;
swapvalue(value1 , value2 );
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
getch();
}
void swapvalue(int a ,int b )
{
int tenp ;
temp = a;
a=b;
b=temp ;
}
void swapreference(int & a , int & b )
{
int temp;
temp = a;
a=b;
b=temp ;
}
void swappointer(int *p , int *q)
{
int * a;
*a=*p;
*p=*q;
*q=*a;
}
इस उदहारण मे swaping function को तीन अलग अलग तरीके से बनाया जाता है |
void swappointer(int *p , int *q); इस function मे pointer variable को use किया जाता है |
void swapvalue(int a ,int b ) ; इस function मे value को use किया जाता है |
void swapreference(int & a , int & b ) ; इस function मे reference variable को use किया जाता है | इसका आउटपुट होगा :
value1 : 100
value2: 200
Swap with reference :
value1 : 200
value2: 100
Swap with pointer :
value1 : 200
value2: 100
Swap with value :
value1 : 200
value2: 100
इस article मे reference variable को discuss किया है | आगे वाले reference variable को दुसरे data type के साथ discuss करेगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…