Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C Pointers · 03 Jul 2026 · Hindi + English

C में NULL Pointer: क्या है, क्यों ज़रूरी है और कैसे Check करें

NULL pointer कहीं point नहीं करता (address 0). हर pointer को NULL से initialize क्यों करें, check कैसे करें और NULL dereference crash क्या है — सब सीखें.

What is a NULL pointer?

A NULL pointer is a pointer that points to nothing — it holds the special value NULL (defined as address 0 in <stdio.h>/<stddef.h>). It is the standard way to say: "this pointer is currently not pointing to any valid memory."
int *p = NULL;      /* safe: clearly points nowhere */

Why initialize pointers to NULL?

An uninitialized pointer contains a random garbage address (a wild pointer). Writing through it corrupts unknown memory. A NULL pointer is safe because:

  • You can test it: if (p == NULL) — with garbage you cannot test anything.
  • Dereferencing NULL crashes immediately and loudly (segmentation fault), so the bug is found fast, instead of silently corrupting data.

How to check a NULL pointer

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(sizeof(int) * 1000000000);  /* may fail */

    if (p == NULL) {                 /* ALWAYS check malloc result */
        printf("Memory allocation failed!\n");
        return 1;
    }

    *p = 10;
    printf("Value: %d\n", *p);
    free(p);
    p = NULL;                        /* good habit after free */
    return 0;
}

Three standard checking styles — all equivalent:

if (p == NULL)  { ... }
if (!p)         { ... }
if (p != NULL)  { ... }   /* or simply: if (p) */

NULL pointer dereference: the crash

int *p = NULL;
*p = 5;              /* CRASH: segmentation fault */
printf("%d", *p);    /* CRASH: reading NULL also crashes */
Rule: before every dereference of a pointer that might be NULL (function parameters, malloc results, linked-list next pointers), check it first. Most real-world C crashes are NULL dereferences.

The free + NULL pattern

free(p);
p = NULL;    /* prevents dangling pointer + double free */

Setting a pointer to NULL right after free() gives two protections: dereferencing it later crashes clearly instead of using freed memory, and calling free(NULL) a second time is guaranteed safe by the C standard.

NULL pointer क्या है?

NULL pointer वह pointer है जो कहीं point नहीं करता — इसमें special value NULL होती है (<stdio.h>/<stddef.h> में address 0 defined). यह कहने का standard तरीका है: "यह pointer अभी किसी valid memory को point नहीं कर रहा."
int *p = NULL;      /* safe: साफ पता है कि कहीं point नहीं करता */

Pointers को NULL से initialize क्यों करें?

Uninitialized pointer में random garbage address होता है (wild pointer). उससे write करना unknown memory corrupt करता है. NULL pointer safe है क्योंकि:

  • आप उसे test कर सकते हैं: if (p == NULL) — garbage को test नहीं कर सकते.
  • NULL dereference तुरंत और साफ crash करता है (segmentation fault), जिससे bug जल्दी मिल जाता है — data चुपचाप corrupt नहीं होता.

NULL pointer कैसे check करें

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(sizeof(int) * 1000000000);  /* fail हो सकता है */

    if (p == NULL) {                 /* malloc result हमेशा check करें */
        printf("Memory allocation failed!\n");
        return 1;
    }

    *p = 10;
    printf("Value: %d\n", *p);
    free(p);
    p = NULL;                        /* free के बाद अच्छी habit */
    return 0;
}

तीन standard checking styles — सब equivalent:

if (p == NULL)  { ... }
if (!p)         { ... }
if (p != NULL)  { ... }   /* या सिर्फ: if (p) */

NULL pointer dereference: crash

int *p = NULL;
*p = 5;              /* CRASH: segmentation fault */
printf("%d", *p);    /* CRASH: NULL पढ़ना भी crash करता है */
Rule: ऐसे pointer को dereference करने से पहले जो NULL हो सकता है (function parameters, malloc results, linked-list के next pointers), पहले check करें. Real-world C crashes में सबसे ज़्यादा NULL dereference ही होते हैं.

free + NULL pattern

free(p);
p = NULL;    /* dangling pointer + double free से बचाता है */

free() के तुरंत बाद pointer को NULL करने से दो protections मिलती हैं: बाद में dereference करने पर freed memory use होने की बजाय साफ crash होता है, और दूसरी बार free(NULL) call करना C standard के हिसाब से guaranteed safe है.

Frequently Asked Questions

C में NULL pointer क्या है?

NULL pointer वह pointer है जिसमें value NULL (address 0) होती है, यानी वह जानबूझकर किसी valid memory location को point नहीं करता.

NULL pointer dereference करने पर क्या होता है?

NULL dereference undefined behaviour है; ज़्यादातर systems पर program तुरंत segmentation fault के साथ crash हो जाता है.

क्या C में free(NULL) safe है?

हां. C standard guarantee करता है कि free(NULL) कुछ नहीं करता, इसीलिए free के बाद pointer को NULL करना double-free bugs से बचाता है.