Addresses &
Pointers
Learn how C++ uses memory addresses and pointer variables to give your programs direct access to data in memory.
What Is a Memory Address?
Every byte of your computer's RAM has a unique numerical label called an address. Think of it like a street address for a house: the house holds a value, and the address tells you where that value lives.
The \( & \) Operator
The address-of operator \( & \) placed before a variable name returns the memory address where that variable is stored. You do not choose the address: the system assigns it.
What Is a Pointer?
A pointer is a variable whose value is a memory address. Instead of holding a number like 42 or a character like 'A', a pointer holds the address of another variable.
Declaring a Pointer
To declare a pointer, write the data type it will point to, followed by an asterisk \( * \), then the pointer name. You typically initialize it with the address of an existing variable.
The \( * \) Dereference Operator
Once you have a pointer, you can dereference it with the \( * \) operator to access or modify the value at the address it holds. This is the inverse of \( & \).
Core Rule: \( & \) and \( * \) Are Inverses
Variable vs Pointer
| Variable | Pointer | |
|---|---|---|
| What it stores | A data value (42, 3.14, 'A') | A memory address (0x1008) |
| Declaration | \( \text{int x;} \) | \( \text{int* ptr;} \) |
| Read value | Use name: \( \text{x} \) | Dereference: \( *\text{ptr} \) |
| Get address | \( &\text{x} \) | \( \text{ptr} \) (it already is one) |
| Size | Depends on type (4, 8, 1 bytes...) | Always 4 or 8 bytes (32/64-bit) |
Using Pointers: Step by Step
Why Do Pointers Matter?
Example 1: Reading Through a Pointer
Example 2: Modifying Through a Pointer
Example 3: Two Pointers, One Variable
The \( \texttt{nullptr} \) Value
A pointer that doesn't point to anything valid should be set to \( \texttt{nullptr} \). This special value means "this pointer is intentionally empty." Dereferencing a nullptr causes a crash.
Common Mistakes
Quick Reference Table
| Syntax | Meaning | Example Result |
|---|---|---|
| \( \text{int x} = 5; \) | Declare variable with value | \( \text{x} \) is 5 |
| \( &\text{x} \) | Address of x | 0x7A00 |
| \( \text{int* p} = &\text{x}; \) | Pointer p holds address of x | \( \text{p} \) is 0x7A00 |
| \( *\text{p} \) | Value at address p holds | 5 |
| \( *\text{p} = 10; \) | Change value at that address | \( \text{x} \) becomes 10 |
| \( \text{p} = \texttt{nullptr}; \) | Pointer points to nothing | safe empty state |
Question 1 of 5
What does the \( & \) operator return when placed before a variable name?
Question 2 of 5
Given \( \text{int n} = 8;\; \text{int* p} = &\text{n}; \), what does \( *\text{p} \) evaluate to?
Question 3 of 5
After running \( \text{int x} = 3;\; \text{int* p} = &\text{x};\; *\text{p} = 10; \), what is the value of \( \text{x} \)?
Question 4 of 5
What is \( \texttt{nullptr} \) used for in C++?
Question 5 of 5
Which line of code correctly declares a pointer to a \( \texttt{double} \) variable called \( \text{gpa} \)?