Void
pointer क्या है? (What is Void Pointer In C In Hindi)
जैसे की हम जानते है एक पॉइंटर केवल उसी डेटा टाइप वाले वेरिएबल का
एड्रेस स्टोर करता है जिस टाइप का वो होता है | उदाहरण के लिए, यदि हम int पॉइंटर डिक्लेअर करते हैं, तो यह इंट पॉइंटर फ्लोट वेरिएबल
या किसी अन्य प्रकार के वेरिएबल को point नहीं कर सकता है, यानी इस
पॉइंटर की मदद से हम केवल int टाइप वेरिएबल को पॉइंट कर सकता है।
ऐसे में इस समस्या को दूर करने
के लिए सी लैंग्वेज में void pointer का कांसेप्ट आया ।
Void
pointer एक
generic पॉइंटर है जिसका किसी भी डेटा टाइप से कोई सम्बन्ध नहीं होता |
यह
किसी भी टाइप के वेरिएबल का एड्रेस स्टोर कर सकता है और इसे आसानी से किसी भी डेटा
टाइप में टाइप-कास्ट किया जा सकता है।
Void
pointer का
डिक्लेरेशन साधारण पॉइंटर की ही तरह होता है मगर void pointer डिक्लेअर करते समय void
कीवर्ड
का उपयोग किया जाता है |
इसका सिंटेक्स कुछ
ऐसा होता है -:
Syntax
void
*pointer_name;
Example :-
void *ptr;
यहाँ void एक कीवर्ड है जिसका उपयोग करते हुए हम void pointer डिक्लेअर कर रहे है और ptr, एक void pointer है |
Why we use void pointers?
हम void
pointers का
उपयोग इसकी reusability के कारण करते है | Void pointers किसी भी टाइप के
ऑब्जेक्ट का एड्रेस स्टोर कर सकता है और हम indirection operator का उपयोग करके उचित
टाइपकास्टिंग के साथ किसी भी टाइप के ऑब्जेक्ट को पुनः प्राप्त कर सकते हैं।
Example
-:
#include<stdio.h> voidmain()
{ //Declaration & initialization of different pointersinta=6;
floatb=5.2;
charc='J';
void*ptr;
// assigning the address of variable 'a'
ptr=&a;
printf("value of 'a' is : %d",*((int*)ptr));
// assigning the address of variable 'b'
ptr=&b;
printf("\nvalue of 'b' is : %f",*((float*)ptr));
// assigning the address of variable 'c'
ptr=&c;
printf("\nvalue of 'c' is : %c",*((char*)ptr));
}
Output
-:
value of 'a' is : 6value of 'b' is : 5.200000value of 'c' is : J
Explanation
-:
ऊपर दिए गए प्रोग्राम
को आप सबसे पहले एक बार अच्छे से देखे! इस प्रोग्राम में हमने एक void
पॉइंटर
ptr नाम से बनाया है जो अलग अलग टाइप (int, char, float) वाले वेरिएबल्स (a,
b, c) का
एड्रेस बारी बारी पॉइंट करता है और indirection operator तथा उचित टीपकास्टिंग
का उपयोग करते हुए इनके वैल्यूज को वापस लौटता है |
Advantages
of Void Pointers
- malloc() और calloc() फ़ंक्शन void pointer रिटर्न करते हैं, इसलिए इन functions का उपयोग किसी भी डेटा टाइप की
मेमोरी allocate
करने के
लिए किया जाता है।
- Generic functions के कांसेप्ट को implement करने के लिए सी लैंग्वेज में void pointer का उपयोग किया जाता है |
- Void pointer का उपयोग करके हम एक generic
linked list भी create
कर सकते
हैं।
Important
points
- Void pointer के साथ पॉइंटर अरिथमेटिक
ऑपरेशन्स परफॉर्म नहीं किया जा सकता |
- इसे dereferenced
के रूप
में उपयोग नहीं किया जा सकता है।
आइये इन दोनों बातों
को हम उदाहरण के एक द्वारा समझते है
Dereferencing
a void pointer in C
अन्य पॉइंटर्स
की तरह, हम void
pointers को dereference नहीं कर सकते क्योंकि कंपाइलर के पास पॉइंट ऑब्जेक्ट के
बारे में कोई जानकारी नहीं होती । यदि हम नीचे दिए गए कोड को Compile करने का प्रयास करते हैं तो हमें कंपाइलर error मिलेगी।
#include <stdio.h>
void main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value
which is pointed by ptr pointer : %d",*ptr);
}


Welcome to OnlineGuru2020.blogspot.com, your number one source for all things related to Online Education. We're dedicated to providing you the very best of Digital Education, with an emphasis on E-notes,Download Links in PDF and youtube videos etc
OnlineGuru2020 Founded in [2020] by Sapan Kumar Das (Asst. Proff Ramchandi College Saraipali), OnlineGuru2020.blogspot.com has come a way from its beginnings .When I first started, my passion inspired me to start my own blog, seeing the need for students in lockdown2020
We hope you enjoy our products as much as we enjoy offering them to you. If you have any questions or comments, please don't hesitate to contact us.
Contact Datails:-
Mobile Number:-9826026747
Email:-sapam.online@gmail.com
Sincerely,
Sapan Kumar Das
.
No comments:
Post a Comment