NULL Pointer in C

This C Tutorial Explains NULL Pointer in C Programming with Examples.

What is NULL Pointer in C?
A NULL Pointer in C is a special type of pointer which points to nowhere i.e. not anywhere. Pointers declared in the program but not initialized contain garbage addresses, pointing to locations beyond the storage allocated to program by the compiler. But these are not NULL Pointers.

For example:

/*
 * uninitialized_pointer.c -- Program shows several different uninitialized
 * pointers
 */
#include <stdio.h>
 
int main(void)
{
    int *iptr;    /* iptr declared pointer-to-integer */
    int *ip;      /* ip declared pointer-to-integer */
    char *cp;     /* cp declared pointer-to-character */
    float *fp;    /* fp declared pointer-to-float */
 
    printf("Default Address \"iptr\" holds as %p\n", iptr);
    printf("Default Address \"ip\" holds as %p\n", ip);
    printf("Default Address \"cp\" holds as %p\n", cp);
    printf("Default Address \"fp\" holds as %p\n", fp);
 
    return 0;
}

Program Output:

Default Address "iptr" holds as (nil)
Default Address "ip" holds as 0x7fff69f3bd80
Default Address "cp" holds as 0x400420
Default Address "fp" holds as (nil)

O key! Now, look at how can we make a pointer NULL and understand its purpose in a program? Consider an example, below,

advertisement
advertisement
/* null_pointer.c -- Program shows how to make a pointer NULL */
#include <stdio.h>
 
int main(void)
{
    int *nptr = 0;     /* making nptr a NULL pointer */
    char *cp = 0;      /* making cp a NULL pointer */
    float *fp = 0;     /* making fp a NULL pointer */ 
 
    printf("In exp. /"int *nptr = 0/"\n");
    if (!nptr)         /* test if nptr is NULL or not */
        printf("nptr is a NULL pointer!\n\n");
    else
        printf("\nnptr is not a NULL pointer!\n\n");
 
    printf("In exp. /"char *cp = 0/"\n");
    if (!cp)           /* test if cp is NULL or not */
        printf("cp is a NULL pointer!\n\n");
    else
        printf("cp is not a NULL pointer!\n\n");
 
    printf("In exp. /"float *fp = 0/"\n");
    if (!fp)           /* test if fp is NULL or not */
        printf("fp is a NULL pointer!\n\n");
    else
        printf("fp is not a NULL pointer!\n\n");
 
    return 0;
}

See the output below:

In exp. "int *nptr = 0"
nptr is a NULL pointer!
 
In exp. "char *cp = 0"
cp is a NULL pointer!
 
In exp. "float *fp = 0"
fp is a NULL pointer!

We observed in above program, we saw how assigning ZERO ‘0’ to pointer to any type, made it a NULL Pointer. This is a source code convention. Internally, however, the value for NULL pointer actually be something different and compiler takes care of translating between zero and internal value.

Remember that Null pointer points to nowhere in memory so NEVER Dereference it for this causes Segmentation Fault.

advertisement

Basically, any uninitialized pointer, if in a program, is made NULL and then used differently, for ex. function can return NULL if searching an array for a particular value and not found, or set last element of an array of pointers as NULL for ease of accessing the array.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.