Skip to content
C++ Better Explained
Go back
C++ nullptr vs NULL: What's the Difference (Beginner Guide)
Updated:

C++ nullptr vs NULL: What’s the Difference

When a pointer doesn’t point at anything yet, you give it a “null” value. For decades C++ programmers used NULL for this, but C++11 introduced nullptr as a safer replacement. Understanding why the change happened will make you a more careful pointer user.


Video Walkthrough


The Problem with NULL

NULL is not a special keyword — it’s a macro that, in most compilers, expands to the plain integer 0. So when you write:

int* p = NULL;

the compiler really sees int* p = 0;. That works for assigning pointers, but it creates a hidden problem: NULL is fundamentally an integer, not a pointer. Most of the time you never notice, until function overloading enters the picture.

Pointers still feel slippery? C++ Better Explained draws pointers and references out with diagrams and plain-English analogies, so you can see what your code is pointing at.

Get the book — $27

A Real Bug NULL Causes

Suppose you have two functions with the same name but different parameter types:

void process(int x);    // overload 1
void process(char* s);  // overload 2

Now you call process(NULL) intending to use the pointer version. This goes wrong in one of two ways depending on how your compiler defines NULL:

Either way, process(NULL) is a trap: it’s a wrong call or an outright error, and which one you get isn’t even portable across compilers. This is exactly the mess nullptr was created to fix.


How nullptr Fixes It

nullptr has its own dedicated type, std::nullptr_t, which is convertible to any pointer type but not to int. Swap it in and the right function gets called:

process(nullptr);  // called process(char*) — correct!

Because nullptr can only mean “a null pointer,” the compiler unambiguously chooses the pointer overload. The intent is crystal clear to both the compiler and the next person reading your code.


Everyday Usage

In normal pointer code, you use nullptr exactly where you used to use NULL:

#include <iostream>

int main() {
    int* ptr = nullptr;        // pointer to nothing

    if (ptr == nullptr) {
        std::cout << "ptr is null, not safe to dereference\n";
    }

    int value = 42;
    ptr = &value;              // now it points somewhere
    if (ptr != nullptr) {
        std::cout << "ptr points to " << *ptr << "\n";  // 42
    }
    return 0;
}

Checking if (ptr == nullptr) before dereferencing is the habit that prevents crashes. A pointer that’s null and gets dereferenced causes undefined behavior — often a segmentation fault.


Quick Comparison

FeatureNULLnullptr
Real typeInteger 0std::nullptr_t
Overload-safeNoYes
IntroducedC / old C++C++11
RecommendedLegacy onlyAlways

The rule is simple: in any C++11 or newer code, always write nullptr. It says exactly what you mean, avoids subtle overload bugs, and is the universal modern standard.



Take Your C++ Further

C++ Better Explained ebook cover

Pointers and references are where most C++ beginners get stuck, and they're far easier once you can picture them. C++ Better Explained explains them with diagrams and analogies, then builds on them to cover memory and OOP.

Get the book — $27

87 pages · plain English · 30-day money-back guarantee

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
using namespace std: What It Means and Why to Avoid It (C++)
Next Post
How to Pass an Array to a Function in C++ (The Right Way)

Keep Learning