NULL Pointer Indirection in C with Examples

This C Tutorial Explains Indirection of a NULL Pointer in C Programming and Describes Pros and Cons of Null Pointer, with examples.

As we know that a NULL pointer is one that points to nowhere in memory. For example:

    int *ip = 0; /* 'ip' is a pointer-to-int initialized with value zero */

In the above pointer initialization, pointer ‘ip’ is made NULL by assigning value zero. So, what will happen if we perform Indirection or Dereferencing upon such a pointer? Indirection is to “go to the location pointed to by the pointer and access the location for value there.” But NULL pointer is not pointing to any location; where to go in memory to access value over there? We take an example to understand this:

/* 
 * indirection_nullptr.c -- Program shows how to make null pointer and what
 * happens on its indirection
 */
#include <stdio.h>
 
int main(void)
{
    int *nptr = 0;               /* making nptr a NULL pointer */
 
    printf("In exp. \"int *nptr = 0\"\n");
    if (!nptr)
        printf("nptr is a NULL pointer!\n");
    else
        printf("nptr is not a NULL pointer!\n");
 
    printf("\nNow we try Indirection '*' on NULL Pointer \'nptr\' to see "
           "what happens...\n");
    printf("The Value at location pointed to by \'nptr\' is %d\n", *nptr);
 
    return 0;
}

Output, when program is run, as below:

advertisement
advertisement
In exp. 'int *nptr = 0'
nptr is a NULL pointer!
 
Now we try Indirection '*' on NULL Pointer 'nptr' to see what happens...
Segmentation fault (core dumped)

Notice here that performing Indirection on NULL Pointer ‘nptr’ caused Segmentation Fault which means we tried to accessing a location that didn’t exist in program’s address space. Therefore, it’s very important to test a pointer for NULL before performing indirection upon it in order to prevent a program from being aborted because of Segmentation fault.

O key! we, now, modify the above program as:

Note: Join free Sanfoundry classes at Telegram or Youtube
/*
 * testfornullptr.c -- Program emphasizes test a pointer for NULL before
 * applying indirection on it
 */
#include <stdio.h>
 
int main(void)
{
    int *nptr = 0;                /* making the 'nptr' a NULL pointer */
 
    printf("In exp. \'int *nptr = 0\'\n");
        if (!nptr) {              /* If 'nptr' is a NULL or not */
            printf("\'nptr\' is a NULL pointer!\n");
            printf("BEWARE! DON'T PERFORM INDIRECTION UPON IT!\n");
        }
        else {
            printf("\'nptr\' is not a NULL pointer!\n");
            printf("\nNow we try Indirection '*' on NULL Pointer \'nptr\'"
                   " and see what happens...\n");
            printf("The Value at location pointed to by 'nptr' is %d\n",
            *nptr);
        }
        return 0;
}

Here is Output,

In exp. 'int *nptr = 0'
'nptr' is a NULL pointer!
BEWARE! DON'T PERFORM INDIRECTION UPON IT!

Remember, firstly, in any program, make any uninitialized pointer a NULL Pointer. Then always test for NULL before performing indirection upon such a pointer to prevent the program from being aborted by Seg Fault.

advertisement

Pros and Cons

An array of pointers can set its last pointer ‘NULL’ for ease of accessing its elements. Example:

    char *str[] = {"o key! ", "this is an ", "array of ",
                     "pointers to characters!", 0};

A function, for example, searching an array for a particular character can return NULL pointer if desired character isn’t found.

advertisement

Null pointers are used to mark end of linked lists for their easy traversing.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

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.