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

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.


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.


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.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

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

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

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.


Edit page
Share this post on:

Previous Post
C++ getline: How to Read a Full Line of Input (With Spaces)
Next Post
C++ Palindrome Program: Check Strings and Numbers for Beginners