NULL pointer in C
At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are
- To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
int* pInt = NULL;chevron_rightfilter_none - To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.
if(pInt != NULL)/*We could use if(pInt) as well*/{/*Some code*/}else{/*Some code*/}chevron_rightfilter_none - To pass a null pointer to a function argument when we don’t want to pass any valid memory address.
intfun(int* ptr){/*Fun specific stuff is done with ptr here*/return10;}fun(NULL);chevron_rightfilter_none
Pointer to a Null Pointer
As Null pointer always points to null, one would think that Pointer to a Null Pointer is invalid and won’t be compiled by the compiler. But it is not the case.
Consider the following example:
#include <stdio.h> int main() { // Null pointer char* np = NULL; // Pointer to null pointer char** pnp = &np; if (*pnp == NULL) { printf("Pointer to a null pointer is valid\n"); } else { printf("Pointer to a null pointer is invalid\n"); } return 0; } |
Not only this program compiles but executes successfully to give the output as
Output:
Pointer to a null pointer is valid
Explanation:
What happens here is that when a Null pointer is created, it points to null, without any doubt. But the variable of Null pointer takes some memory. Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null.
Hence Pointer to a null pointer is not only valid but important concept.
Recommended Posts:
- Difference between NULL pointer, Null character ('\0') and '0' in C with Examples
- Double Pointer (Pointer to Pointer) in C
- NULL pointer in C
- Pointer to an Array | Array Pointer
- Passing NULL to printf in C
- Dangling, Void , Null and Wild Pointers
- NULL undeclared error in C/C++ and how to resolve it
- How to declare a pointer to a function?
- Pointer vs Array in C
- Declare a C/C++ function returning pointer to array of integer pointers
- 'this' pointer in C++
- Type of 'this' pointer in C++
- Multidimensional Pointer Arithmetic in C/C++
- When do we pass arguments by reference or pointer?
- How to write C functions that modify head pointer of a Linked List?
- Function Pointer in C
- Scope Resolution Operator Versus this pointer in C++?
- Opaque Pointer
- Passing by pointer Vs Passing by Reference in C++
- void pointer in C / C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

