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
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 03. 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
#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
p is a null pointer, cannot be dereferenced safely.
p now points to a valid value: 256. Key Takeaways
- A null pointer holds no valid address; it points to nothing.
- Modern C++ uses
nullptr(type-safe, C++11+); older code uses theNULLmacro (often just0). - Dereferencing a null pointer is undefined behavior and typically crashes the program.
- Always compare a pointer to
nullptrbefore dereferencing it if it might be null.
Practice what you learned
1. What is a null pointer?
2. Which keyword does modern C++ (C++11 and later) recommend for representing a null pointer?
3. What is a drawback of using the old `NULL` macro instead of `nullptr`?
4. What happens if you dereference a null pointer, e.g. `*p` where `p == nullptr`?
5. What is a good practice before dereferencing a pointer that might be null?
Was this page helpful?
You May Also Like
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Dangling Pointers in C++
Understand how a pointer becomes 'dangling' after its target is freed or goes out of scope, and the safe practices that prevent it.
Dynamic Memory Allocation in C++
Learn how `new` and `delete` allocate and free heap memory at runtime, and why forgetting to `delete` causes a memory leak.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics