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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.