Thuta Learning
IntermediateProgrammingbeginner

Pointers

Relax. We'll talk through this in plain words — no textbook voice.

A pointer is a variable that stores another variable's memory address. Pointers are what give C++ that close, hands-on control over memory. It can feel a little intimidating at first, but once you get comfortable separating an address from a value, it starts to click.

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string food = "Pizza";
    string* ptr = &food;

    cout << "Value: " << food << endl;
    cout << "Address: " << &food << endl;
    cout << "Pointer stores: " << ptr << endl;
    cout << "Value through pointer: " << *ptr;
    return 0;
}

&food grabs the memory address of the food variable. ptr stores that address. *ptr retrieves the actual value sitting at the address the pointer holds.

You should see
Value: Pizza Address: 0x... Pointer stores: 0x... Value through pointer: Pizza

Info

& gets an address, while * does double duty for pointer declaration and dereference depending on context — keep the two straight.

Easy traps

  • Dereferencing a pointer that doesn't hold a valid address can crash your program. Whenever you work with pointers, keep ownership and lifetime in mind.
Pointers | Thuta Learning