What is nullptr in C++? What is the difference from NULL?

nullptr means “points to nothing”. It has the type std::nullptr_t. The old NULL is just the number 0, which can confuse overloaded functions.

#include <iostream>

void f(int)   { std::cout << "int\n"; }
void f(char*) { std::cout << "pointer\n"; }

int main() {
    f(0);              // int
    f(nullptr);        // pointer
    int* p = nullptr;
    std::cout << (p == nullptr) << '\n';
}
Output

int
pointer
1

Always initialize pointers with a value or nullptr. See Pointers.


Learn more about the similar topics:
Tutorials
C++ References and const References
C++ Pointers
C++ Dynamic Memory: new, delete and Smart Pointers
Exercises & Assignments
Swap two numbers using a reference function
Manage a resource with unique_ptr
Interview Questions & Answers
What is the difference between a reference and a pointer in C++?
What is the difference between new and malloc in C++?
What is a memory leak in C++ and how to avoid it?
What is nullptr in C++? What is the difference from NULL?
What is RAII in C++?
What is the difference between unique_ptr, shared_ptr and weak_ptr?
What is const correctness in C++?