`nullptr` — C++ Keyword

`nullptr` — C++ Keyword

The nullptr keyword in C++11: a type-safe null pointer constant.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

nullptr

A type-safe null pointer constant of type std::nullptr_t, introduced in C++11. Replaces the ambiguous NULL macro and the integer literal 0 in pointer contexts.

Syntax

Type* ptr = nullptr;
if (ptr == nullptr) { ... }
func(nullptr);    // unambiguously passes a null pointer, not 0

Example

#include <print>

void process(int* p) {
    if (p == nullptr) {
        std::println("null pointer");
        return;
    }
    std::println("{}", *p);
}

// Overload resolution: nullptr -> pointer overload, not int overload
void overloaded(int)   { std::println("int");     }
void overloaded(int*)  { std::println("pointer"); }

int main() {
    int x = 42;
    process(&x);       // 42
    process(nullptr);  // null pointer

    // overloaded(0);       // ambiguous
    overloaded(nullptr);    // unambiguously selects pointer overload
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}