01 / 21
ptr val

Declaring & Using
Pointers in C++

Master the * and & operators to manipulate memory addresses, pass data efficiently, and write safe, powerful C++ code.

C++

What Is a Pointer?

Every variable in C++ lives at a specific memory address: a numeric location in RAM. A pointer is a variable that stores the address of another variable instead of storing a data value directly.

* Dereference
Access the value at the address a pointer holds.
& Address-of
Retrieve the memory address of an existing variable.

How Memory Works

0x1000 0x1004 0x1008 0x100C ? ? 42 ? x 0x1008 ptr points to

Why Pointers Matter

Efficiency
Pass large objects by address instead of copying them.
Dynamic Memory
Allocate memory at runtime with new and delete.
Data Structures
Build linked lists, trees, and graphs using node pointers.

The Syntax Rules

int x = 42;           // a normal integer variable
int* ptr = &x;       // ptr holds the address of x

cout << ptr;          // prints the address, e.g. 0x1008
cout << *ptr;         // prints 42 (the value at that address)

* vs & : Know the Difference

* (Dereference) & (Address-of)
Purpose Access value at an address Get the address of a variable
Operand Takes a pointer Takes a variable
Returns The stored value (lvalue) A memory address
Example *ptr → 42 &x → 0x1008

The Inverse Relationship

x 42 ptr 0x1008 & *
\(*(\&x) == x \quad \&(*ptr) == ptr\)

How to Use Pointers Safely

1
Declare the pointer with the correct type: int* ptr;
2
Initialize it to the address of a valid variable: ptr = &x;
3
Dereference to read or write the value: *ptr = 99;
4
Never dereference an uninitialized or null pointer: always check before using.
5
Set to nullptr when the pointer should point to nothing: ptr = nullptr;

Example 1: Reading via a Pointer

int score = 95;
int* p = &score;

cout << "Address: " << p   << endl;  // 0x7ffc...
cout << "Value:   " << *p  << endl;  // 95
score 95 p &score *p → 95 0x7ffc9a20

Example 2: Writing via a Pointer

int score = 95;
int* p = &score;

*p = 100;                // write through the pointer
cout << score;            // prints 100 — the original changed!

Example 3: Swap with Pointers

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int x = 5, y = 10;
swap(&x, &y);
cout << x << " " << y;  // prints: 10 5

The nullptr Keyword

int* ptr = nullptr;     // points to nothing

if (ptr != nullptr) {
    cout << *ptr;        // safe: only dereference if not null
} else {
    cout << "No data";
}

Pointers and Arrays

int arr[] = {10, 20, 30, 40};
int* p = arr;           // p points to arr[0]

cout << *p;             // 10
cout << *(p + 1);      // 20
cout << *(p + 2);      // 30
[0] [1] [2] [3] 10 20 30 40 p
\(p+1\)
\(p+2\)
\(p+3\)
sizeof(int)

Type Matters

int    a = 10;
double b = 3.14;

int*    pa = &a;   // OK: int pointer to int
double* pb = &b;   // OK: double pointer to double

// int* pc = &b;       // ERROR: type mismatch!

Common Mistakes

✕ Uninitialized Pointer
Declaring int* p; without assigning an address: dereferencing causes a crash or random data.
✕ Dangling Pointer
Keeping a pointer after the variable it points to has gone out of scope or been deleted.
✕ Confusing * Contexts
Mixing up int* p (declaration) with *p (dereference). They look the same but mean different things.

Summary Cheat Sheet

Syntax Meaning Example Result
int* p; Declare a pointer to int p is uninitialized
p = &x; Store address of x in p p → 0x1008
*p Read value at address p 42
*p = 99; Write through pointer x becomes 99
p = nullptr; Point to nothing Safe "empty" state
p + 1 Next element in array Moves by sizeof(int)

Question 1 of 5

Given int x = 7; int* p = &x;, what does *p evaluate to?

Question 2 of 5

Which operator returns the memory address of a variable?

Question 3 of 5

What happens if you dereference a pointer set to nullptr?

Question 4 of 5

Given int a = 3; int* q = &a; *q = 8;, what is the value of a after this code runs?

Question 5 of 5

Given int arr[] = {5, 10, 15}; int* p = arr;, what does *(p + 2) evaluate to?