Loading...

C++ Pointers

The references page left one line on the screen in box.cpp: pine, after std::string& alias = word; and alias = "pine";. This page stays in the Memory and Collections module. The workshop file on disk stays box.cpp. The new work stores the address of word in a pointer, then changes the value through that address. The screen still shows one line: pine.
A pointer holds an address of another object that already exists. On this page you write std::string word = "cone"; and then std::string* p = &word;. The * in the type marks a pointer variable. The & before word takes the address of word and stores it in p. Then *p = "pine"; writes pine through that address into word. Printing word shows pine on the screen. Reading std::string* p = &word is reading a stored address, not a second name like a reference from the last page.
Programs use pointers when you need to pass or keep an address and decide later what to do with it. You do not keep the reference alias as the lesson path on this page, even though both tools can reach the same word. Creating a null pointer waits for the next page in this module. Here the point is only taking an address with & and writing through *p after that address is stored. 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.

Inside main, write std::string word = "cone";, then std::string* p = &word;, then *p = "pine";, then std::cout << word << std::endl;, then return 0;. Keep the includes for iostream and string. Do not write std::string& alias on this page. Do not assign "pine" only to a separate copy and ignore the pointer.
Walk the run carefully from the first line of main. The program starts at main. word starts as cone. p stores the address of that string object. *p = "pine"; follows the address and stores pine into word. Printing word shows pine on the screen. Then main returns and the program ends. There is one line on the screen. It is pine, not cone. If you printed *p instead, you would still see pine, because the pointer points at the same storage after the write. The checked file prints word on purpose so the original name proves the change went through the pointer address.

If you write *p = "pine"; before p holds an address, the program is unsafe. That failed experiment stays in the prose. The checked file always sets p = &word before any *p write.
If you write p = "pine"; without the star, the build fails because p holds an address, not a string value. The checked file always writes through *p.
A common mistake is to mix the reference form std::string& with the pointer form on the same line. Another common mistake is to forget & when storing the address, so p never points at word. A third mistake is to print p itself and expect the text pine. A fourth mistake is to leave word as cone and never write through *p. A fifth mistake is to drop #include <string> after bringing std::string back.
This page stops before the next tool, where a pointer can hold no object yet. The next page names that tool once at the end. The pointer you have just written only shows an address and a write through *p in the same function. Keep the address line before any star write on purpose so the order stays clear for a first careful reading of the file.

Open box.cpp. Replace the reference alias lines with a pointer p. Keep iostream and string. In main, create word, set p to &word, write *p = "pine";, and print word. The file name on disk stays box.cpp. The checked file is below, and the new lines are the pointer declaration and the write through *p.

box.cpp points p at word then writes pine through *p

cpp
#include <iostream>
#include <string>

int main()
{
    std::string word = "cone";
    std::string* p = &word;
    *p = "pine";
    std::cout << word << 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. Writing through *p changed word at that address in memory. 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 shows cone, you may have skipped the *p write entirely. If the build fails about assigning a string to p, put the star back on the write line so you write through the pointer.

📊 pointer and what the screen shows

Piece What the screen shows
First pine, because *p = "pine" updates word through the address
Last pine, because the program prints one line and stops
Rule a pointer stores an address; *p reaches the object at that address
Screen pine

Try one change at a time. Change *p = "pine"; to *p = "oak";, leave the address line as it is, and rebuild. The screen shows oak instead of pine, because the pointer still points at word. Put "pine" back. Next, temporarily comment out *p = "pine"; and rebuild to see cone again, then restore the write so the file returns to the checked shape. Each experiment changes one thing on purpose, so the screen shows exactly whether the write through the address ran. Undo each change before the next one starts, so the file always returns to the checked workshop version with the pointer write fully restored once again after every single careful try back on disk again.
This page ends the pointers tool and stops before the next one. The next page is nullptr, which names a pointer that holds no object yet. The pointer you have just written only shows storing the address &word and then writing through *p 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.

🧠 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