What is a Pointer to a Null pointer

16 Jun, 2022
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. 
To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. 
C
int* pInt = NULL;
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. 
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. 
C
if (pInt != NULL) /*We could use if(pInt) as well*/{ /*Some code*/}else { /*Some code*/}
To pass a null pointer to a function argument when we don’t want to pass any valid memory address. 
To pass a null pointer to a function argument when we don’t want to pass any valid memory address. 
C
int fun(int* ptr){    /*Fun specific stuff is done with ptr here*/    return 10;}fun(NULL);
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: 
C++
C
#include <iostream>using namespace std; int main(){     // Null pointer    char* np = NULL;     // Pointer to null pointer    char** pnp = &np;     if (*pnp == NULL) {        cout << "Pointer to a null pointer is valid" << endl;    }    else {        cout << "Pointer to a null pointer is invalid" << endl;    }     return 0;} // This code is contributed by sarajadhav12052009
#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.
sarajadhav12052009
Advanced Pointer
C-Advanced Pointer
C-Pointer Basics
C-Pointers
cpp-double-pointer
cpp-pointer
pointer
Pointers
C Programs
Pointers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
C Program to read contents of Whole File
How to return multiple values from a function in C or C++?
C++ Program to check Prime Number
Producer Consumer Problem in C
C Program to Swap two Numbers
How to Append a Character to a String in C
C program to sort an array in ascending order
Program to find Prime Numbers Between given Interval
Set, Clear and Toggle a given bit of a number in C