Loading...

C++ nullptr

The pointers page left one line on the screen in box.cpp: pine, after std::string* p = &word; and *p = "pine";. This page stays in the Memory and Collections module. The workshop file on disk stays box.cpp. The new work starts the pointer with no object, then stores an address before any print through the star. The screen still shows one line: pine.
The word nullptr means a pointer holds no object address yet. On this page you write std::string* p = nullptr; first on purpose. Then you create std::string word = "pine"; and assign p = &word; in a later line. Only after that assignment do you print *p safely. Reading nullptr is reading an empty pointer value, not an address of word. The Classes cast is gone from this file; this is still the pointer tool with one safer first step before any star use.
Programs use nullptr so a pointer can exist before you know which object it should reach. You do not write through *p while it is still nullptr on this page, and you do not leave the pointer empty forever in the checked run. Creating a vector waits for the next page in this module. Here the point is only the empty start, the later address store, and the print after that store is done. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <iostream> and #include <string> together at the top. You compile only box.cpp again for this workshop file on disk.

Inside main, write std::string* p = nullptr; first. Then write std::string word = "pine";, then p = &word;, then std::cout << *p << std::endl;, then return 0;. Keep the includes for iostream and string. Do not print *p before p = &word;. Do not keep the old *p = "pine"; write as the only lesson path.
Walk the run carefully from the first line of main. The program starts at main. p begins as nullptr, so it holds no object address yet. word is created with the text pine. p = &word; stores that address in p after the empty start. Printing *p follows the address and shows pine on the screen. Then main returns and the program ends. There is one line on the screen. It is pine. The empty start matters on purpose: you only dereference after the address exists. The checked file always assigns &word before *p is used for output in this workshop.

If you print *p while p is still nullptr, the program is unsafe. That failed experiment stays in the prose. The checked file always stores an address before any star read.
If you write p = 0; instead of nullptr, older code might compile, but this page uses nullptr as the clear empty value. The checked file keeps nullptr.
A common mistake is to dereference right after nullptr with no address assignment. Another common mistake is to leave p as nullptr forever and never print. A third mistake is to mix the reference form std::string& back in as the lesson path. A fourth mistake is to print p itself and expect the text pine. A fifth mistake is to drop #include <string> after bringing std::string back.
This page stops before the next tool, where a growing list lives in a vector. The next page names that tool once at the end. The nullptr start you have just written only shows an empty pointer that later receives &word in the same function. Keep the empty line first on purpose so the later address assignment stays visible for a first reading.

Open box.cpp. Replace the immediate &word bind with nullptr first, then create word as pine, assign p = &word;, and print *p. Keep iostream and string. The file name on disk stays box.cpp. The checked file is below, and the new lines are the nullptr start and the later address assignment.

box.cpp starts p as nullptr then points at word

cpp
#include <iostream>
#include <string>

int main()
{
    std::string* p = nullptr;
    std::string word = "pine";
    p = &word;
    std::cout << *p << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows one line: pine.
Check that line first on the screen. After p = &word, reading *p printed pine from the object at that address. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen is blank, the print may be missing from main. If you crash by reading *p too early, put the address assignment back before the star read so the pointer is never empty at that moment.

📊 nullptr and what the screen shows

Piece What the screen shows
First pine, because p gets &word before *p is printed
Last pine, because the program prints one line and stops
Rule nullptr means no object yet; store an address before you use *p
Screen pine

Try one change at a time. Change word = "pine" to word = "oak", leave the nullptr start and the later p = &word; as they are, and rebuild. The screen shows oak instead of pine, because the pointer still receives the address before the print. Put "pine" back. Next, temporarily move the print above p = &word; only in your head as a warning, then keep the checked order so you never ship that unsafe shape. Each safe experiment changes one thing on purpose, so the screen shows exactly which string address was stored after nullptr. Undo each change before the next one starts, so the file always returns to the checked workshop version with nullptr first and the address assignment before *p once again after every single careful try back on disk again.
This page ends the nullptr tool and stops before the next one. The next page is vector, which holds a growing list of values. The empty pointer start you have just written only shows nullptr before &word in box.cpp. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line on that final screen, and that single printed line on the screen is still just pine alone right there now after the final successful build run on disk today now.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top