Pointer in C in Hindi
C भाषा में pointer एक variable होता है जो दूसरे वेरिएबल के address को स्टोर करता है। यह variable प्रकार int, char, array, function या किसी अन्य पॉइंटर का हो सकता है। pointer का आकार architecture पर निर्भर करता है। हालांकि, 32-bit architecture में एक पॉइंटर का आकार 2 byte है।
एक pointer को परिभाषित करने के लिए निम्नलिखित उदाहरण पर विचार करें जो integer के address को संग्रहीत करता है।
- int n = 10;
- int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer in c in hindi
C भाषा में pointer को * (asterisk चिह्न) का उपयोग करके घोषित किया जा सकता है। इसे indirection pointer के रूप में भी जाना जाता है जिसका उपयोग एक pointer को निष्क्रिय करने के लिए किया जाता है।
- int *a;//pointer to int
- char *c;//pointer to char
Pointer Example
पते और मूल्य को प्रिंट करने के लिए पॉइंटर्स का उपयोग करने का एक उदाहरण नीचे दिया गया है।
जैसा कि आप उपरोक्त आंकड़े में देख सकते हैं, पॉइंटर वेरिएबल नंबर वैरिएबल का address स्टोर करता है, अर्थात, fff4 संख्या variable का मान 50 है। लेकिन pointer variable p का पता aaa3 है।
* ( indirection operator) की मदद से , हम पॉइंटर वैरिएबल p के मान को प्रिंट कर सकते हैं।
उपरोक्त आंकड़े के लिए समझाए गए pointer उदाहरण को देखें।
- #include<stdio.h>
- int main(){
- int number=50;
- int *p;
- p=&number;//stores the address of number variable
- printf(“Address of p variable is %x \n”,p); // p contains the address of the number therefore printing p gives the address of number.
- printf(“Value of p variable is %d \n”,*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
- return 0;
- }
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Pointer to array in c in hindi
- int arr[10];
- int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.
Pointer to a function in c in hindi
- void show (int);
- void(*p)(int) = &display; // Pointer p is pointing to the address of a function
Pointer to structure in c in hindi
- struct st {
- int i;
- float f;
- }ref;
- struct st *p = &ref;
Advantage of pointer in c in hindi
1) pointer कोड कम कर देता है और प्रदर्शन को बेहतर बनाता है, यह strings, trees, आदि को पुन: प्राप्त करने के लिए इस्तेमाल किया और arrays, structures, और functions के साथ प्रयोग किया जाता है।
2) हम पॉइंटर का उपयोग करके एक फ़ंक्शन से कई मान वापस कर सकते हैं ।
3) यह आपको कंप्यूटर की मेमोरी में किसी भी मेमोरी लोकेशन को एक्सेस करने में सक्षम बनाता है ।
Usage of pointer in hindi
C भाषा में पॉइंटर्स के कई applications हैं।
1) Dynamic memory allocation
सी भाषा में, हम dynamically रूप से malloc() और calloc() फ़ंक्शन का उपयोग कर मेमोरी आवंटित कर सकते हैं जहां पॉइंटर का उपयोग किया जाता है।
2) Arrays, Functions और Structures
C भाषा में पॉइंटर्स का उपयोग arrays, functions और structures में व्यापक रूप से किया जाता है। यह कोड को कम करता है और प्रदर्शन में सुधार करता है।
Address Of (&) Operator in hindi
ऑपरेटर & का पता एक variable का पता देता है। लेकिन, हमें varible का पता प्रदर्शित करने के लिए %u का उपयोग करने की आवश्यकता है।
- #include<stdio.h>
- int main(){
- int number=50;
- printf(“value of number is %d, address of number is %u”,number,&number);
- return 0;
- }
Output
value of number is 50, address of number is fff4
NULL Pointer in c in hindi
एक पॉइंटर जिसे कोई मान नहीं दिया गया है को NULL पॉइंटर के रूप में जाना जाता है। यदि आपके पास घोषणा के समय पॉइंटर में निर्दिष्ट किया जाने वाला कोई पता नहीं है, तो आप NULL मान असाइन कर सकते हैं। यह एक बेहतर दृष्टिकोण प्रदान करेगा।
int *p=NULL;
अधिकांश libraries में, pointer का मान 0 (शून्य) है।
Pointer Program to swap two numbers without using the 3rd variable.
- #include<stdio.h>
- int main(){
- int a=10,b=20,*p1=&a,*p2=&b;
- printf(“Before swap: *p1=%d *p2=%d”,*p1,*p2);
- *p1=*p1+*p2;
- *p2=*p1-*p2;
- *p1=*p1-*p2;
- printf(“\nAfter swap: *p1=%d *p2=%d”,*p1,*p2);
- return 0;
- }
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Reading complex pointers
सी में complex pointers को पढ़ते समय कई बातों को ध्यान में रखा जाना चाहिए। आइए हम operators की precedence और associativity को देखें जो ponters के संबंध में उपयोग की जाती हैं।
Operator | Precedence | Associativity |
(), [] | 1 | Left to right |
*, identifier | 2 | Right to left |
Data type | 3 | – |
यहाँ, हमें ध्यान देना चाहिए कि,
- (): यह ऑपरेटर एक ब्रैकेट ऑपरेटर है जिसका उपयोग फ़ंक्शन को घोषित करने और परिभाषित करने के लिए किया जाता है।
- []: यह ऑपरेटर एक array सबस्क्रिप्ट ऑपरेटर है
- *: यह ऑपरेटर एक pointer ऑपरेटर है।
- identifier: यह pointer का नाम है। प्राथमिकता हमेशा इसे सौंपी जाएगी।
- Data type: डेटा प्रकार उस variable का प्रकार है जिसके लिए सूचक को point करना है। इसमें modigier जैसे signed int, long आदि भी शामिल हैं।
How to read the pointer: int (*p) [10]
सूचक को पढ़ने के लिए, हमें यह देखना होगा कि () और [] समान precedence है। संबद्धता को left to right ओर है, इसलिए प्राथमिकता () में जाती है।
ब्रैकेट() के अंदर, पॉइंटर ऑपरेटर * और पॉइंटर नाम (identifier) p की एक ही precedence है। इसलिए, उनकी associativity पर विचार किया जाना चाहिए जो कि right से left है, इसलिए प्राथमिकता p पर जाती है, और दूसरी प्राथमिकता * पर जाती है।
डेटा प्राथमिकता के अंतिम होने के बाद से [] को तीसरी प्राथमिकता दें। इसलिए पॉइंटर निम्न जैसा दिखेगा।
- char -> 4
- * -> 2
- p -> 1
- [10] -> 3
पॉइंटर को पढ़ा जाएगा क्योंकि p 10 आकार के पूर्णांकों की एक array के लिए एक पॉइंटर है।
Example
How to read the following pointer?
- int (*p)(int (*)[2], int (*)void))
Explanation
इस पॉइंटर को p पढ़ा जाएगा क्योंकि p एक ऐसे फंक्शन का पॉइंटर है, जो पहले पैरामीटर को पॉइंटर के रूप में स्वीकार करता है, जो कि आकार दो के पूर्णांक के one-dimentional array के लिए है और दूसरा पैरामीटर जो किसी फ़ंक्शन के लिए पॉइंटर के रूप में है, जो पैरामीटर शून्य और रिटर्न प्रकार integer है।