Explain NULL Pointer in C with Examples

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

A NULL Pointer in C is a special type of pointer which points to nowhere i.e. not anywhere. Pointers declared in 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;
}

Output:

advertisement
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,

/* 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:

advertisement
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, how we, by 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.

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.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn