NULL Pointer in C++ (original) (raw)

Last Updated : 8 Jun, 2026

A NULL pointer in C++ represents a pointer that does not refer to any valid memory address.

#include using namespace std;

int main(){ int* ptr = nullptr;

if (ptr == nullptr) {
    cout << "Pointer is currently null." << endl;
}
else {
    cout << "Pointer is not null." << endl;
}

// *ptr = 10; (to avoid runtime error)
// Assigning a valid memory address to the pointer
int value = 5;
ptr = &value;

// Checking if the pointer is null after assigning a
// valid address
if (ptr == nullptr) {
    cout << "Pointer is currently null." << endl;
}
else {
    cout << "Pointer is not null." << endl;
    cout << "Value at the memory location pointed to "
            "by the pointer: "
         << *ptr << endl;
}

return 0;

}

`

Output

Pointer is currently null. Pointer is not null. Value at the memory location pointed to by the pointer: 5

**Explanation:

Syntax

We can create a NULL pointer of any type by simply assigning the value NULL to the pointer as shown:

int* ptrName = NULL; // before C++11
int* ptrName = nullptr;
int* ptrName = 0; // using the null pointer constant 0

A null pointer can be initialized using the null pointer constants 0, NULL, or (since C++11) nullptr. In modern C++, nullptr is the preferred way to represent a null pointer because it is type-safe.

Checking NULL Pointer

We can check whether a pointer is a NULL pointer by using the equality comparison operator.

ptrName == NULL
or
ptrName == nullptr

The above expression will return true if the pointer is a NULL pointer. False otherwise.

Applications of Null Pointer in C++

Null pointers serve several important purposes in C++, from safe initialization of pointers to error handling and resource management.

Issues with NULL

NULL pointer makes it possible to check for pointer errors but it also has its limitations: