← Back to C Course | Chapter 7: Pointers | Lesson 8 of 9

C का void Pointer

एक void pointer किसी भी type के data की ओर point कर सकता है, किसी भी चीज़ पर चिपकाए जा सकने वाले एक blank label की तरह। यह जिसकी ओर point करता है उसे इस्तेमाल करने से पहले आपको बताना होगा कि यह असल में किस type का है।
Syntax
c
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?

c
// 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;
}

void Pointers को Addresses Assign करना

यह flexibility exactly वह चीज़ है जो void pointers को generic containers या functions (जैसे qsort() या memcpy()) लिखने के लिए useful बनाती है जिन्हें हर type के लिए दोबारा लिखे बिना किसी भी type के data के साथ काम करना है।

उदाहरण: Assigning Addresses to void Pointers

c
// 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;
}

एक void Pointer Dereference करना

इसे dereference करने से पहले आपको एक void pointer को वापस एक concrete type में cast करना होगा -- जैसे int *p = (int*)voidPtr -- क्योंकि compiler को जानना होता है कि कितने bytes पढ़ने हैं और उन्हें कैसे interpret करना है।

उदाहरण: Dereferencing a void Pointer

c
// 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;
}

Functions में void Pointers

यह generality type safety की कीमत पर आती है: function के पास यह verify करने का कोई तरीका नहीं है कि caller ने उम्मीद वाला type pass किया या नहीं, इसलिए यहाँ bugs आमतौर पर सिर्फ runtime पर सामने आते हैं।

उदाहरण: void Pointers in Functions

c
// 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;
}

void Pointers पर Pointer Arithmetic

यह restriction इसलिए है क्योंकि compiler को नहीं पता कि void* किसकी ओर point कर रहा है उसका size क्या है, इसलिए इसके पास यह calculate करने का कोई तरीका नहीं कि 'एक element आगे' actually कितनी दूर है -- आपको पहले एक concrete pointer type में cast करना होगा।

उदाहरण: Pointer Arithmetic on void Pointers

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक void* को सीधे dereference करना, जब पहले इसे cast करना ज़रूरी है।
  2. एक void* पर pointer arithmetic करना, जो standard नहीं है।
  3. गलत type में cast करना और garbage पढ़ना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.