Menu
Coddy logo textTech

Pointers and References

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 13 of 104.

Now that you understand stack and heap memory, let's explore the tools that let you work with memory locations directly: pointers and references.

A pointer stores the memory address of another variable. You declare it with * and get an address using &.

int value = 42;
int* ptr = &value;    // ptr holds the address of value

std::cout << ptr;     // Prints the memory address
std::cout << *ptr;    // Dereference: prints 42

A reference is an alias for an existing variable. Once bound, it always refers to that variable. You declare it with &.

int value = 42;
int& ref = value;     // ref is an alias for value

ref = 100;            // Changes value to 100
std::cout << value;   // Prints 100

Key differences:

PointersReferences
Can be nullMust be initialized
Can be reassignedCannot be rebound
Use * to access valueAccess value directly
Can point to nothingAlways valid

Both are essential for passing objects to functions efficiently without copying, and for working with heap-allocated memory. References are safer and simpler, while pointers offer more flexibility when you need to reassign or check for null.

challenge icon

Challenge

Easy

Let's build a simple value manipulation system that demonstrates how pointers and references work differently when modifying data.

You'll create two files to organize your code:

  • ValueOps.h: Define three functions that modify integer values in different ways:
    • doubleByPointer(int* ptr) — takes a pointer and doubles the value it points to
    • tripleByReference(int& ref) — takes a reference and triples the value
    • swapValues(int* a, int* b) — takes two pointers and swaps the values they point to
  • main.cpp: Read two integers from input, then demonstrate both pointers and references:
    • Print the original values: "Original: <first> <second>"
    • Use doubleByPointer on the first value, then print: "After double: <first>"
    • Use tripleByReference on the second value, then print: "After triple: <second>"
    • Use swapValues to swap both values, then print: "After swap: <first> <second>"

When calling functions with pointers, remember to use the address-of operator (&) to pass the variable's address. References work more naturally — just pass the variable directly.

Include your header file in main.cpp using #include "ValueOps.h".

Cheat sheet

A pointer stores the memory address of another variable. Declare it with * and get an address using &:

int value = 42;
int* ptr = &value;    // ptr holds the address of value

std::cout << ptr;     // Prints the memory address
std::cout << *ptr;    // Dereference: prints 42

A reference is an alias for an existing variable. Declare it with &:

int value = 42;
int& ref = value;     // ref is an alias for value

ref = 100;            // Changes value to 100
std::cout << value;   // Prints 100

Key differences:

PointersReferences
Can be nullMust be initialized
Can be reassignedCannot be rebound
Use * to access valueAccess value directly
Can point to nothingAlways valid

When calling functions with pointers, use the address-of operator (&) to pass the variable's address. References work directly — just pass the variable.

Try it yourself

#include <iostream>
#include "ValueOps.h"
using namespace std;

int main() {
    // Read two integers from input
    int first, second;
    cin >> first;
    cin >> second;
    
    // Print original values
    cout << "Original: " << first << " " << second << endl;
    
    // TODO: Use doubleByPointer on first value (pass address using &)
    
    // TODO: Print after double
    
    // TODO: Use tripleByReference on second value (pass variable directly)
    
    // TODO: Print after triple
    
    // TODO: Use swapValues to swap both values (pass addresses using &)
    
    // TODO: Print after swap
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming