C++ nullptr Keyword

The nullptr keyword in C++ is a null pointer constant introduced in C++11. It provides a type-safe way to represent a null pointer, replacing the traditional NULL macro and the literal 0 used in earlier versions of C++.

Using nullptr eliminates ambiguity in function overloading and improves code readability and type safety when working with pointers.


Syntax

</>
Copy
data_type* pointer_name = nullptr;
data_type
The type of the pointer, such as int, double, etc.
pointer_name
The name of the pointer variable being declared.
nullptr
A null pointer constant representing the absence of a valid memory address.

Examples

Example 1: Initializing a Pointer with nullptr

In this example, you will learn how to use nullptr to initialize a pointer.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int* ptr = nullptr; // Initialize pointer with nullptr

    if (ptr == nullptr) {
        cout << "The pointer is null." << endl;
    }

    return 0;
}

Output:

The pointer is null.

Explanation:

  1. The pointer ptr is initialized with nullptr, indicating it does not point to any valid memory location.
  2. The if statement checks whether ptr is null, and the corresponding message is printed.

Example 2: Using nullptr in Function Overloading

In this example, you will learn how nullptr resolves ambiguity in function overloading.

</>
Copy
#include <iostream>
using namespace std;

void print(int* ptr) {
    if (ptr == nullptr) {
        cout << "Null pointer passed." << endl;
    } else {
        cout << "Pointer value: " << *ptr << endl;
    }
}

void print(int value) {
    cout << "Integer value: " << value << endl;
}

int main() {
    int* ptr = nullptr;
    print(ptr); // Calls the first overload

    int value = 42;
    print(value); // Calls the second overload

    return 0;
}

Output:

Null pointer passed.
Integer value: 42

Explanation:

  1. Two print functions are defined: one accepts a pointer, and the other accepts an integer.
  2. Using nullptr in print(ptr) ensures the correct function overload (pointer version) is called.
  3. When print(value) is called, the integer version of the function executes.

Example 3: Assigning nullptr to Reset a Pointer

In this example, you will learn how to reset a pointer using nullptr.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int value = 10;
    int* ptr = &value;

    cout << "Pointer before reset: " << *ptr << endl;

    ptr = nullptr; // Reset the pointer

    if (ptr == nullptr) {
        cout << "Pointer has been reset to nullptr." << endl;
    }

    return 0;
}

Output:

Pointer before reset: 10
Pointer has been reset to nullptr.

Explanation:

  1. The pointer ptr initially points to the address of value.
  2. The pointer is reset using nullptr, making it a null pointer.
  3. The if condition confirms that the pointer has been reset.

Key Points about nullptr Keyword

  1. The nullptr keyword provides a type-safe way to represent a null pointer.
  2. It replaces older methods like NULL or 0, which could lead to ambiguity in function calls.
  3. Using nullptr improves code readability and eliminates common pointer-related bugs.
  4. It can be assigned to pointers of any type.
  5. Introduced in C++11, nullptr is the recommended way to handle null pointers in modern C++ code.