100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Null Pointers in C++

Learn what a null pointer is, why modern C++ prefers `nullptr` over the old `NULL` macro, and why dereferencing one causes undefined behavior.

PointersIntermediate6 min readJul 7, 2026
Analogies

1. Intro

A null pointer is a pointer that does not point to any valid memory location — it holds no usable address. Null pointers are used to indicate that a pointer is "empty" or not currently pointing at any object, and checking for them is a common way to guard against invalid memory access.

🏏

Cricket analogy: A scoreboard slot for 'Man of the Match' before the award ceremony holds no valid player yet — it's empty, just as a null pointer holds no valid memory address until it's actually assigned one.

2. Syntax

cpp
int* p = nullptr;   // modern C++ (C++11 onward) - preferred
int* q = NULL;       // old C-style macro - legacy code
int* r = 0;           // also valid but discouraged, ambiguous with int 0

3. Explanation

In modern C++ (C++11 and later), the keyword nullptr is the preferred way to represent a null pointer. nullptr has its own type, std::nullptr_t, which the compiler treats strictly as a pointer value — this avoids ambiguity in overload resolution and template code. Older C++ and C code often used the NULL macro instead, which is typically just #define NULL 0, meaning it can be confused with the integer 0 in some contexts (for example, overload resolution between int and pointer parameters). Because nullptr is type-safe, it is strongly preferred in new code; NULL is still seen in legacy codebases. Regardless of which is used, dereferencing a null pointer (writing *p when p is null) is undefined behavior — it typically crashes the program (a segmentation fault) because address 0 is not a valid, accessible memory location. Always check a pointer against nullptr before dereferencing it if there is any chance it could be null.

🏏

Cricket analogy: Modern DRS, like nullptr in C++11+, is precise, unlike the old NULL macro, really just 0, easily mistaken for a legitimate zero score, and just as walking out to an imaginary pitch would be disastrous, dereferencing a null pointer crashes the program.

Dereferencing a null pointer (*p when p == nullptr) is undefined behavior and will typically crash your program with a segmentation fault. Always check if (p != nullptr) before dereferencing a pointer that might be null.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int* p = nullptr;

    if (p == nullptr) {
        cout << "p is a null pointer, cannot be dereferenced safely." << endl;
    }

    int x = 25;
    p = &x;    // now p points to a valid variable

    if (p != nullptr) {
        cout << "p now points to a valid value: " << *p << endl;
    }

    return 0;
}

5. Output

text
p is a null pointer, cannot be dereferenced safely.
p now points to a valid value: 25

6. Key Takeaways

  • A null pointer holds no valid address; it points to nothing.
  • Modern C++ uses nullptr (type-safe, C++11+); older code uses the NULL macro (often just 0).
  • Dereferencing a null pointer is undefined behavior and typically crashes the program.
  • Always compare a pointer to nullptr before dereferencing it if it might be null.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#NullPointersInC#Null#Pointers#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep