01 / 21

Addresses &
Pointers

Learn how C++ uses memory addresses and pointer variables to give your programs direct access to data in memory.

C++

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.

3.14 42 'A' 7.5 0x1000 0x1008 0x100C 0x1018 pi score letter rate &score = 0x1008

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.

int score = 42; cout << "Value : " << score << endl; // 42 cout << "Address: " << &score << endl; // 0x7ffd...

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.

score 42 0x1008 ptr 0x1008 0x2040 points to int int* ptr stores the address of score

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.

int score = 42; int* ptr = &score; // ptr now holds address of score double gpa = 3.9; double* gPtr = &gpa; // gPtr points to gpa
\( \text{int*} \)
Pointer to an int: can only store the address of an int variable.
\( \text{double*} \)
Pointer to a double: can only store the address of a double variable.
\( \text{char*} \)
Pointer to a char: can only store the address of a char 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 \( & \).

int score = 42; int* ptr = &score; cout << *ptr << endl; // 42 (read the value) *ptr = 100; // change value through pointer cout << score << endl; // 100 (score itself changed!)

Core Rule: \( & \) and \( * \) Are Inverses

score 42 ptr 0x1008
\( & \)
address-of
\( * \)
dereference
\( *(\&\text{score}) == \text{score} \qquad \&(*\text{ptr}) == \text{ptr} \)

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

1
Declare a regular variable and give it a value: \( \text{int n} = 10; \)
2
Declare a pointer of the same type: \( \text{int* p;} \)
3
Assign the address to the pointer: \( \text{p} = &\text{n}; \)
4
Read the value via dereferencing: \( *\text{p} \) gives 10
5
Modify the value through the pointer: \( *\text{p} = 20; \) now \( \text{n} == 20 \)

Why Do Pointers Matter?

Efficient Functions
Pass large data by address instead of copying it, saving time and memory.
Dynamic Memory
Allocate memory at runtime with \( \texttt{new} \) and free it with \( \texttt{delete} \).
Data Structures
Linked lists, trees, and graphs all rely on pointers to connect nodes.

Example 1: Reading Through a Pointer

int age = 17; int* p = &age; cout << age << endl; // 17 cout << &age << endl; // 0x3A00 cout << p << endl; // 0x3A00 cout << *p << endl; // 17
age 17 0x3A00 p 0x3A00 0x5C10
\( *\text{p} \)

Example 2: Modifying Through a Pointer

int lives = 3; int* ptr = &lives; cout << lives << endl; // 3 *ptr = 5; // modify lives through ptr cout << lives << endl; // 5 (changed!) cout << *ptr << endl; // 5

Example 3: Two Pointers, One Variable

int val = 7; int* p1 = &val; int* p2 = &val; // both point to the same place *p1 = 20; cout << *p2 << endl; // 20 (p2 sees the change!) cout << val << endl; // 20
val 20 0x4000 p1 0x4000 p2 0x4000

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.

int* ptr = nullptr; // ptr points to nothing if (ptr != nullptr) { cout << *ptr; // safe: only dereference if not null } else { cout << "No target!"; }

Common Mistakes

✕ Uninitialized Pointer
Declaring \( \text{int* p;} \) without assigning an address. The pointer holds garbage, and dereferencing it causes undefined behavior.
✕ Type Mismatch
Assigning an int variable's address to a \( \text{double*} \) pointer. The pointer type must match the variable type.
✕ Confusing \( * \) Contexts
Mixing up \( \text{int* p} \) (declaration) with \( *\text{p} \) (dereference). In declarations, \( * \) means "pointer." In expressions, \( * \) means "go to address."

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} \)?