← Back to C++ Course | Chapter 7: Pointers & References | Lesson 10 of 11

C++ के Null और Void Pointer

एक null pointer कहीं की ओर point नहीं करता, एक envelope पर एक empty address जैसा, और एक void pointer किसी भी type की ओर point कर सकता है, एक blank label की तरह। आपको इन्हें सही तरीके से check और cast करने में careful रहना ज़रूरी है।
Syntax
cpp
data_type *pointer_name = nullptr;

if (pointer_name != nullptr) {
  // safe to use *pointer_name
}

Null Pointer क्या है?

एक null pointer एक pointer है जो जानबूझकर किसी valid memory address की ओर point नहीं करता, 'यहाँ अभी कोई object नहीं' represent करने के लिए इस्तेमाल होता है।

Modern C++ इसके लिए पुराने NULL macro की बजाय nullptr keyword इस्तेमाल करता है, क्योंकि nullptr का अपना type है और इसे integer 0 से चुपचाप confuse नहीं किया जा सकता।

उदाहरण: What is a Null Pointer?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int *ptr = nullptr;
	// Print to the console with cout
	std::cout << "ptr points to nothing yet" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Check करना

इसे dereference करने से पहले हमेशा check करें कि एक pointer null है या नहीं, क्योंकि एक null pointer से पढ़ना या इसमें लिखना undefined behavior है और typically आपके program को तुरंत एक segmentation fault से crash कर देता है।

यह एक habit pointer-heavy code में सबसे common runtime bugs में से एक रोकती है।

उदाहरण: Checking for Null

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int *ptr = nullptr;
	// Check whether ptr != nullptr
	if (ptr != nullptr) {
		// Print to the console with cout
		std::cout << *ptr << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Cannot dereference: pointer is null" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Void Pointer क्या है?

एक void pointer (void*) एक generic pointer है जो किसी भी data type के variable का address hold कर सकता है, जो इसे type-agnostic low-level utilities लिखने के लिए useful बनाता है।

हालांकि, क्योंकि compiler अब नहीं जानता यह किस type की ओर point करता है, आप एक void pointer को सीधे dereference नहीं कर सकते।

उदाहरण: What is a Void Pointer?

cpp
#include <iostream>

int main() {
	int num = 5;
	void *vptr = &num; // can hold the address of any type
	std::cout << "Void pointer set" << std::endl;
	return 0;
}

Void Pointers Cast करना

Actually एक void pointer के refer किए data को पढ़ने के लिए, आपको पहले इसे वापस इसके real, specific type में cast करना होगा, आमतौर पर modern C++ में static_cast से।

यह step skip करना और void pointer को as-is dereference करना compile भी नहीं होगा, क्योंकि compiler को नहीं पता कितने bytes पढ़ने हैं।

उदाहरण: Casting Void Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare num and set it to 42
	int num = 42;
	void *vptr = &num;
	int *intPtr = static_cast<int *>(vptr);
	// Print to the console with cout
	std::cout << *intPtr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Common Pitfalls

एक void pointer को गलत type में cast करने से बचें, क्योंकि यह program को एक incorrect size और layout इस्तेमाल करके memory reinterpret कराता है, garbage values या corruption produce करते हुए।

यह भी कभी void या null pointer पर पहले check या cast किए बिना सीधे dereferencing syntax apply न करें।

उदाहरण: Common Pitfalls

cpp
#include <iostream>

int main() {
	double num = 3.14;
	void *vptr = &num;
	int *wrongPtr = static_cast<int *>(vptr); // wrong type: misreads the bytes
	std::cout << "Casting to the wrong type corrupts the value read" << std::endl;
	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. एक null pointer dereference करना, जो program crash करता है।
  2. जहाँ nullptr safer है वहाँ NULL या 0 इस्तेमाल करना, जो गलत overload चुन सकता है।
  3. एक void* को सीधे dereference करना, जब इसे पहले एक specific pointer type में cast करना ज़रूरी है।

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.