C का void Pointer
In this page:
void *pointer_name = &variable_name;
*(data_type *)pointer_name; // cast before dereferencing
void Pointer क्या है?
क्योंकि यह कोई type information नहीं रखता, compiler सीधे इस पर कोई type-specific operations perform नहीं कर सकता, लेकिन यही चीज़ इसे malloc() जैसे functions के लिए natural choice भी बनाती है जिन्हें किसी भी type की memory वापस देनी होती है।
उदाहरण: What is a void Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
void *ptr;
// Declare x and set it to 5
int x = 5;
ptr = &x;
// Print formatted text to the screen
printf("%d", *(int*)ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
void Pointers को Addresses Assign करना
यह flexibility exactly वह चीज़ है जो void pointers को generic containers या functions (जैसे qsort() या memcpy()) लिखने के लिए useful बनाती है जिन्हें हर type के लिए दोबारा लिखे बिना किसी भी type के data के साथ काम करना है।
उदाहरण: Assigning Addresses to void Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Declare f and set it to 3.5f
float f = 3.5f;
void *ptr;
ptr = &x;
ptr = &f;
// Print formatted text to the screen
printf("%.1f", *(float*)ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक void Pointer Dereference करना
इसे dereference करने से पहले आपको एक void pointer को वापस एक concrete type में cast करना होगा -- जैसे int *p = (int*)voidPtr -- क्योंकि compiler को जानना होता है कि कितने bytes पढ़ने हैं और उन्हें कैसे interpret करना है।
उदाहरण: Dereferencing a void Pointer
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 42
int x = 42;
void *voidPtr = &x;
int *p = (int*)voidPtr;
// Print formatted text to the screen
printf("%d", *p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions में void Pointers
यह generality type safety की कीमत पर आती है: function के पास यह verify करने का कोई तरीका नहीं है कि caller ने उम्मीद वाला type pass किया या नहीं, इसलिए यहाँ bugs आमतौर पर सिर्फ runtime पर सामने आते हैं।
उदाहरण: void Pointers in Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printAsInt(void *data) {
// Print formatted text to the screen
printf("%d", *(int*)data);
}
// Program execution starts in main()
int main() {
// Declare x and set it to 7
int x = 7;
printAsInt(&x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
void Pointers पर Pointer Arithmetic
यह restriction इसलिए है क्योंकि compiler को नहीं पता कि void* किसकी ओर point कर रहा है उसका size क्या है, इसलिए इसके पास यह calculate करने का कोई तरीका नहीं कि 'एक element आगे' actually कितनी दूर है -- आपको पहले एक concrete pointer type में cast करना होगा।
उदाहरण: Pointer Arithmetic on void Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
void *ptr = arr;
int *typedPtr = (int*)ptr;
typedPtr++;
// Print formatted text to the screen
printf("%d", *typedPtr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक
void*को सीधे dereference करना, जब पहले इसे cast करना ज़रूरी है। - एक
void*पर pointer arithmetic करना, जो standard नहीं है। - गलत type में cast करना और garbage पढ़ना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: