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:
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:
/* * 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.
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.
Null pointers are used to mark end of linked lists for their easy traversing.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice BCA MCQs
- Check C Books