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

C का Null Pointer

एक null pointer कहीं की ओर point नहीं करता, एक envelope पर एक empty address जैसा। इस्तेमाल से पहले इसे check करना आपके program को crash होने से रोकता है।
Syntax
c
data_type *pointer_name = NULL;

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

Null Pointer क्या है?

C में यह conventional 'यह pointer जानबूझकर कहीं नहीं point करता' signal है, इसे एक initial safe default और एक sentinel value दोनों की तरह इस्तेमाल किया जाता है जिसे malloc() और fopen() जैसे functions failure बताने के लिए return करते हैं।

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Null Pointer Initialize करना

ज़्यादातर implementations में NULL असल में सिर्फ integer 0 को एक pointer type में cast करके define किया जाता है, इसलिए int *ptr = NULL और int *ptr = 0 functionally equivalent हैं, हालांकि NULL लिखना readers को intent ज़्यादा clearly बताता है।

उदाहरण: Initializing a Null Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Safe Pointer Checking

यह C में defensive programming 101 है -- क्योंकि language में कोई automatic null-checking नहीं है, यह check भूल जाना segmentation faults के सबसे common कारणों में से एक है, खासकर एक malloc() या fopen() call के तुरंत बाद जो fail हो सकती थी।

उदाहरण: Safe Pointer Checking

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Check whether ptr != NULL
	if (ptr != NULL) {
		// Print formatted text to the screen
		printf("%d", *ptr);
	} else {
		// Print formatted text to the screen
		printf("Cannot dereference a null pointer");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointer बनाम Uninitialized Pointer

एक uninitialized pointer एक null वाले से कहीं ज़्यादा dangerous है क्योंकि यह accidentally valid-looking memory की ओर point कर सकता है और काम करता हुआ दिख सकता है, bug को तब तक छिपाते हुए जब तक यह कुछ important corrupt न कर दे -- हमेशा pointers को explicitly initialize करें, NULL को भी।

उदाहरण: Null Pointer vs. Uninitialized Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *safePtr = NULL;
	// Print formatted text to the screen
	printf("%d", safePtr == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Return Values के तौर पर Null Pointers

यह convention calling code को एक single return value इस्तेमाल करके 'operation succeeded, ये है आपका result' को 'operation failed' से अलग करने देता है, यही कारण है कि इसे इस्तेमाल करने से पहले एक function के return pointer को NULL के against check करना standard practice है।

उदाहरण: Null Pointers as Return Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* find(int value) {
	// Check whether value != 5
	if (value != 5) {
		// Send NULL back to the caller
		return NULL;
	}
	static int found = 5;
	// Send &found back to the caller
	return &found;
}
// Program execution starts in main()
int main() {
	int *result = find(3);
	// Print formatted text to the screen
	printf("%d", result == NULL);
	// 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. एक null pointer को dereference करना, जो program crash करता है।
  2. एक uninitialized pointer इस्तेमाल करना इसे NULL set करने की बजाय।
  3. इस्तेमाल करने से पहले एक pointer को NULL के लिए check न करना।

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.