Coding/Learn/cpp/pointers memory addresses
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnUnmasking Memory: Pointers & Addresses (& and *)
18 / 20 0%
Lesson 18 of 20
6 mins read

Unmasking Memory: Pointers & Addresses (& and *)

PreviousNext Lesson
In Plain English

Pointers are the superpower that gives C++ unmatched speed and hardware control. Every variable sits at a physical address in RAM (e.g. 0x7ffd5e). The & operator retrieves a variable's address, and a pointer variable (int *ptr) stores that address. The * dereference operator accesses the value at that address.

The Mental Model

"Think of a house. The furniture inside is the variable value (e.g. 42). The street address written on the mailbox (e.g. "742 Evergreen Terrace") is the memory address (&). A pointer is a piece of paper with that address written down. Looking at the paper gives you the address; driving to the address and opening the door is dereferencing (*)."

Deep Dive: How It Works

Address-Of Operator (&): &var returns the hexadecimal memory location where var lives in RAM.

Pointer Declaration (*): int *ptr = &var; (ptr is a pointer variable storing the memory address of an integer).

Dereference Operator (*): *ptr reads or writes the actual value sitting at that address (*ptr = 99; changes the original variable!).

nullptr: Always initialize pointers to nullptr if they don't point to a valid address yet, preventing dangerous wild pointer bugs.

Syntax Blueprint

int score = 100;
int *ptr = &score; // 1. Store address
std::cout << *ptr; // 2. Dereference (prints 100)
*ptr = 200;        // 3. Mutates score to 200!

& gets memory address, int * creates pointer, * dereferences target.

PointCore Rules to Remember

Point star
The Address Operator (&): Retrieves the physical memory location of any variable in RAM.
Point star
Dereferencing (*): Follows the address pointer to inspect or mutate the underlying value.
Point star
Nullptr Safety: Never dereference a null or uninitialized pointer (causes immediate crash/segfault).

Common Beginner Traps & How to Fix Them

Mistake:Confusing int *ptr = &val with *ptr = val

Why it happens: The asterisk (*) means two different things: creating a pointer type vs dereferencing an address.

How to fix: In declaration, * denotes pointer type. In expressions, * means "go to this address".

Pro Tip: In modern C++, smart pointers (std::unique_ptr, std::shared_ptr) automate memory management and eliminate memory leaks!

Live Interactive Example

Hit Run Code to see it live
Pointers and Dereferencing in Action
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: *ptr = 150 follows the stored address and updates the gold variable directly in memory.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Mutate a variable via pointer

  • PointDeclare int health = 80; and int *ptr = &health;
  • PointSet *ptr = 100;
  • PointPrint "Health: 100".
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates