Course outline · 0% complete

0/27 lessons0%

Course overview →

Addresses and Pointers

lesson 5-1 · ~13 min · 13/27

Quiz

Warm-up from lesson 4-2: `void doubleIt(int& x)` could modify the caller's variable. What did the & mean there?

Memory is addressed — and C++ lets you use the addresses

This unit exists because the structures interviews love — linked lists, trees, graphs — are built from values that refer to other values, and in C++ that referring mechanism is explicit. It is also the layer that finally explains crashes: segmentation faults, dangling references, and even Python's object model all become concrete once you can see addresses.

The actual hardware rule first: RAM is byte-addressable. Every byte of memory has a unique number, its address, and the CPU reads and writes data by giving that number. While your program runs, every variable therefore lives at some address. (If a mental image helps: a gigantic row of numbered one-byte boxes.) An int occupies 4 consecutive bytes, and "the address of x" means the address of its first byte.

C++ lets you work with addresses directly:

  • &x is the address-of operator: the address where x lives. (Same symbol as reference declarations, different meaning by context.)
  • A pointer is a variable whose value is an address. int* p declares p as "pointer to int".
  • *p is the dereference operator: go to the address stored in p and use the int that lives there.
int x = 42;
int* p = &x;   // p now holds x's address

std::cout << *p;   // 42, read x through p
*p = 99;           // write through p
std::cout << x;    // 99, x itself changed

Python hides all of this: every Python name is secretly a pointer-like handle managed for you. C++ hands you the raw mechanism.

int x = 99address 0x5000int* p = 0x5000a pointer stores an address&x gives 0x5000*p follows the arrow to x
A pointer is just a box whose contents are the address of another box. Dereferencing (*p) follows the stored address back to the pointed-at value.

Code exercise · cpp

Run this. Watch x change even though the code only ever assigns through the pointer p.

nullptr, and why pointers exist at all

A pointer that points at nothing should be set to nullptr:

int* p = nullptr;
if (p != nullptr) { std::cout << *p; }   // guard before dereferencing

Dereferencing a null (or otherwise invalid) pointer is undefined behavior, usually a crash. This is the sharpest edge in C++.

Why does the language keep such a dangerous tool?

  • Dynamic memory: memory created at run time (unit 6) is reachable only through a pointer.
  • Data structures: linked lists, trees, and graphs are nodes that point at other nodes.
  • Cheap sharing: passing an 8-byte address instead of copying a large object (references, from lesson 4-2, are a tamer packaging of the same idea).
  • Arrays: an array's name is essentially the address of its first element, as you will see next lesson.

Code exercise · cpp

Your turn. Without touching main, complete `reset_to_zero` so it sets the pointed-at int to 0. The parameter is a pointer, so you must dereference it. Expected output: `before: 55` then `after: 0`.

Code exercise · cpp

Your turn: complete swapValues so it exchanges the two caller variables THROUGH the pointers. The program should print: 7 3