A pointer variable stores the address of another object.

pointer variable A pointer type uses `*` in its declaration, such as `int*`.

Pointers

example
pointers.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

int main() {
    int first = 10;
    int second = 20;
    int* current = &first;

    std::cout << "current=" << *current << std::endl;

    current = &second;

    std::cout << "current=" << *current << std::endl;
    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int first = 4;
    int second = 20;
    int* current = &first;

    std::cout << "current=" << *current << std::endl;

    current = &second;

    std::cout << "current=" << *current << std::endl;
    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int first = 18;
    int second = 20;
    int* current = &first;

    std::cout << "current=" << *current << std::endl;

    current = &second;

    std::cout << "current=" << *current << std::endl;
    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int first = 10;
    int second = 8;
    int* current = &first;

    std::cout << "current=" << *current << std::endl;

    current = &second;

    std::cout << "current=" << *current << std::endl;
    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int first = 10;
    int second = 30;
    int* current = &first;

    std::cout << "current=" << *current << std::endl;

    current = &second;

    std::cout << "current=" << *current << std::endl;
    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    return 0;
}
  1. first ← 10, second ← 20, current ← ⟨addr A⟩

    3int main() {4    int first→ 10 = 10; //@first=4, 185    int second→ 20 = 20; //@second=8, 306    int* current→ ⟨addr A⟩ = &first10;78    std::cout << "current=" << *current⟨addr A⟩ << std::endl;910    current→ ⟨addr B⟩ = &second20;1112    std::cout << "current=" << *current⟨addr B⟩ << std::endl;13    std::cout << "first=" << first10 << std::endl;14    std::cout << "second=" << second20 << std::endl;15    return 0;16}
    outputcurrent=10
    current=20
    first=10
    second=20
  1. first ← 4, second ← 20, current ← ⟨addr A⟩

    3int main() {4    int first→ 4 = 4;5    int second→ 20 = 20;6    int* current→ ⟨addr A⟩ = &first4;78    std::cout << "current=" << *current⟨addr A⟩ << std::endl;910    current→ ⟨addr B⟩ = &second20;1112    std::cout << "current=" << *current⟨addr B⟩ << std::endl;13    std::cout << "first=" << first4 << std::endl;14    std::cout << "second=" << second20 << std::endl;15    return 0;16}
    outputcurrent=4
    current=20
    first=4
    second=20
  1. first ← 18, second ← 20, current ← ⟨addr A⟩

    3int main() {4    int first→ 18 = 18;5    int second→ 20 = 20;6    int* current→ ⟨addr A⟩ = &first18;78    std::cout << "current=" << *current⟨addr A⟩ << std::endl;910    current→ ⟨addr B⟩ = &second20;1112    std::cout << "current=" << *current⟨addr B⟩ << std::endl;13    std::cout << "first=" << first18 << std::endl;14    std::cout << "second=" << second20 << std::endl;15    return 0;16}
    outputcurrent=18
    current=20
    first=18
    second=20
  1. first ← 10, second ← 8, current ← ⟨addr A⟩

    3int main() {4    int first→ 10 = 10;5    int second→ 8 = 8;6    int* current→ ⟨addr A⟩ = &first10;78    std::cout << "current=" << *current⟨addr A⟩ << std::endl;910    current→ ⟨addr B⟩ = &second8;1112    std::cout << "current=" << *current⟨addr B⟩ << std::endl;13    std::cout << "first=" << first10 << std::endl;14    std::cout << "second=" << second8 << std::endl;15    return 0;16}
    outputcurrent=10
    current=8
    first=10
    second=8
  1. first ← 10, second ← 30, current ← ⟨addr A⟩

    3int main() {4    int first→ 10 = 10;5    int second→ 30 = 30;6    int* current→ ⟨addr A⟩ = &first10;78    std::cout << "current=" << *current⟨addr A⟩ << std::endl;910    current→ ⟨addr B⟩ = &second30;1112    std::cout << "current=" << *current⟨addr B⟩ << std::endl;13    std::cout << "first=" << first10 << std::endl;14    std::cout << "second=" << second30 << std::endl;15    return 0;16}
    outputcurrent=10
    current=30
    first=10
    second=30