CodeBlanks Logo Icon CodeBlanks

nullptr

Learn the modern null pointer concept with nullptr.

nullptr

nullptr is the pointer value meaning 'points nowhere'. In old C++ you would use NULL or 0, but modern C++ prefers nullptr because it is type-safe.

Initializing a pointer with int* p = nullptr; states clearly that p has no target yet. Later you can check whether it is empty with if (p == nullptr). This is the safety check before dereferencing.

Null check

Before dereferencing a pointer with *p, it is a good habit to check whether it is null. Dereferencing a null pointer is undefined behavior; the program may crash.

The check: if (p != nullptr) { std::cout << *p; }. This prints the value only if p is not null. Null pointers are an explicit way to express 'no target yet'; so you meet them often in modern C++ code.

Practice Null pointer check

Declare int* p = nullptr;. Check with if (p == nullptr); if true, print "Boş" with std::cout (with a newline).

Starter code

main.cpp
#include <iostream>

int main() {
    // Set p to nullptr
    // Check if it is null
    // Print Boş
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    int* p = nullptr;
    if (p == nullptr) {
        std::cout << "Boş\n";
    }
    return 0;
}

Expected output

Boş

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 nullptr assignment

Question
int* p = ____;
Answer
int* p = nullptr;

Explanation: int* p = nullptr; makes p a null pointer.

2 What is nullptr?

  • A value that points nowhere Correct answer
  • An empty variable
  • A pointer type
  • A reference

Explanation: nullptr is the pointer value that points nowhere.

3 Match each expression to its meaning

Term Meaning
nullptr Null pointer
if (p == nullptr) Check
int* Pointer type
p == nullptr Comparison

All lessons in Beginner