C++ : Pointer in hindi , what is pointer in c++ language with examples computer programe
using namespace std ;
Pointer declaration को समजने के लिए निन्म उदाहरण को discuss किया जाता है :-
using namespace std ;
int *p;
p=&a;
*p=*p+1;
int *p;
इसमें ‘p’ एक pointer है जिसका type integer है | अतः ये integer value के address को hold कर सकता है | इसके अलावा किसी दुसरे type की pointer variable को declare करने के लिए :
char *c;
double *d;
char *c;-इसमें ‘c’ एक pointer है जिसका type character है | अतः ये character value के address को hold कर सकता है |
इस उदाहरण के आउटपुट मे , *p और ‘a’ की value सामान होती है क्योकि p pointer variable है जो की variable ‘a’ की address को hold करता है *p से pointer variable मे store address पर स्थित value को access किया जाता है |
किसी pointer variable मे value को assign करने के लिए *p का use किया जाता है |
इसका आउटपुट होगा :
Enter value : 12
value of ‘a’ : 12
Address of ‘a’ : 0x0065fd40
value of ‘p’ : 12
Address of ‘p’ : 0x0065fd42
value of ‘p’ : 13
इसके अलावा निन्म rules को भी pointer declartion मे consider किया जाता है :-
pointer declaration मे space को use करना थोडा confusion होता है | अतः इसे declare करने के लिए निन्म rules को define किया जाता है :-
1.data type और * के बीच space हो सकता है |
int *p;
2.* और variable के बीच space हो सकता है |
int* p;
3.लेकिन अगर हम निन्म syntax को use करते है तब
int* p, a;
यहा पर ‘p’ एक pointer variable है लेकिन ‘a’ एक pointer variable नहीं है |
4.अगर अलग अलग pointer को define करने के लिए अलग अलग declarion को use किया जा सकता है :-
int *p;
char *c;
यह int ये भी define कर रहा ‘p’ की size 4 bytes होगी |
और char define करता है ‘a’ की size 2 bytes होगी |
Pointer Danger
pointer के use बहुत संभाल के करना चाहिए | जब हम किसी pointer variable को declare करते है तब ये ध्यान मे रखना चाहिए :- pointer variable एक memory address को store करने के लिए space को generate करता है इसमें value assign नहीं होती है | उदाहरण के लिए
int *p;
*p=1200;
ये syntax possible नहीं क्योकि pointer को initial ही नहीं किया गया है |1200 भी एक address नहीं है | 1200 , pointer variable मे store address पर assign होगा |
pointer का उदाहरण :
using namespace std ;
int *p;
int *b;
p=&a;
b=&c;
इस उदाहरण मे , addition operation को pointer variable से solve किया गया है |
इस article मे हमने pointer के basic को discuss किया है |अब आगे के article मे pointer के advance topic को discuss करेगे |