C का Null Pointer
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Safe Pointer Checking
यह C में defensive programming 101 है -- क्योंकि language में कोई automatic null-checking नहीं है, यह check भूल जाना segmentation faults के सबसे common कारणों में से एक है, खासकर एक malloc() या fopen() call के तुरंत बाद जो fail हो सकती थी।
उदाहरण: Safe Pointer Checking
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक null pointer को dereference करना, जो program crash करता है।
- एक uninitialized pointer इस्तेमाल करना इसे
NULLset करने की बजाय। - इस्तेमाल करने से पहले एक pointer को
NULLके लिए check न करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: