TeachMate
My ProgressJoin Session

Pointers & References

0/8 completed · ~0h 33m

All Topics
C++LearnPointers & ReferencesSyllabus
1 / 8

Pointers

intermediate
4 min read

What you'll learn

  • Explain what a memory address and a pointer are.
  • Dereference a pointer and check it before use.
  • Explain why a dangling pointer is undefined behavior.

A pointer stores the memory address of another variable. Pointers are powerful but require careful handling — they enable dynamic memory management, data structures, and low-level system programming.

Start With a Problem

A function must change the caller's variable. If it receives a copy, the caller's value never changes. Passing the variable's address lets the function reach the original:

void addOne(int* value) { (*value)++; }  // reach the original through its address

int n = 10;
addOne(&n);   // pass the address of n
// n is now 11

To work with an address you need a type that stores one: a pointer. The rest of this note explains addresses, dereferencing, and safe use.

Use the hotel analogy: memory is a hotel of numbered rooms, a pointer is a slip of paper with a room number, and *p means 'go to that room'. A null pointer is a blank slip that names no room — it points to no object, and dereferencing it is undefined behavior.

Why Pointers Matter

  • Dynamic memory allocation (new/delete)
  • Efficient passing of large data structures
  • Building data structures (linked lists, trees, graphs)
  • Interfacing with operating systems and hardware

Declaring Pointers

int x = 10;
int* p = &x;  // p stores the address of x

The & Operator (Address-Of)

int x = 10;
int* p = &x;  // p = 0x7ffd5e8a3c4c (some address)

The * Operator (Dereference)

cout << *p;    // 10 (go to address, read value)
*p = 20;       // x is now 20

Memory Visualization

int x = 10;    // Variable x, value 10, address 0x100
int* p = &x;   // Pointer p, value 0x100, address 0x200

Memory:
Address  Value
0x100    10       (x)
0x200    0x100    (p)
A pointer stores the address of a variableint score = 420x1000int* p = &score0x1000points to
The variable stores a value at an address; the pointer stores that same address.

Null Pointer

int* p = nullptr;  // Points to nothing
if (p != nullptr) {
    cout << *p;  // Only dereference non-null pointers
}

Pointer Arithmetic

int arr[5] = {10, 20, 30, 40, 50};
int* p = arr;      // p points to arr[0]
*(p + 1);          // 20 (arr[1])
*(p + 2);          // 30 (arr[2])
p++;                // p now points to arr[1]

Common Mistakes

  • Dereferencing null pointer: *nullptr (crash)
  • Dangling pointer: p points to freed memory
  • Uninitialized pointer: int* p; *p = 5; (wild pointer)

Never use an uninitialized pointer. Always set it to nullptr or point to a valid variable before dereferencing.

Key takeaway: Pointers store memory addresses — use & to get an address and * to dereference.

Try it: Declare an int and a pointer to it, print the value through the pointer, then change the value via *p and print the variable again to show they refer to the same memory.

TeachMate - Interactive learning platform for bachelor students

HomeLearnJoin Session|2 Subjects