A pointer stores the address of another object.

address-of The `&` operator produces the address where a variable is stored.
pointer value An `int *` variable can hold the address of an `int`.

Addresses

value
addresses.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

int main(void) {
    int value = 12;
    int *ptr = &value;
    int same = (*ptr == value);

    printf("same=%d\n", same);
    return 0;
}
#include <stdio.h>

int main(void) {
    int value = 7;
    int *ptr = &value;
    int same = (*ptr == value);

    printf("same=%d\n", same);
    return 0;
}
#include <stdio.h>

int main(void) {
    int value = 20;
    int *ptr = &value;
    int same = (*ptr == value);

    printf("same=%d\n", same);
    return 0;
}
  1. value ← 12, ptr ← ⟨addr A⟩, same ← 1

    3int main(void) {4    int value→ 12 = 12; //@value=7, 205    int *ptr→ ⟨addr A⟩ = &value12;6    int same→ 1 = (*ptr⟨addr A⟩ == value12);78    printf("same=%d\n", same1);9    return 0;10}
    outputsame=1
  1. value ← 7, ptr ← ⟨addr A⟩, same ← 1

    3int main(void) {4    int value→ 7 = 7;5    int *ptr→ ⟨addr A⟩ = &value7;6    int same→ 1 = (*ptr⟨addr A⟩ == value7);78    printf("same=%d\n", same1);9    return 0;10}
    outputsame=1
  1. value ← 20, ptr ← ⟨addr A⟩, same ← 1

    3int main(void) {4    int value→ 20 = 20;5    int *ptr→ ⟨addr A⟩ = &value20;6    int same→ 1 = (*ptr⟨addr A⟩ == value20);78    printf("same=%d\n", same1);9    return 0;10}
    outputsame=1

Follow the Address

  1. Start with one normal int variable.
  2. Use &value to ask where that variable lives.
  3. Store that address in an int * pointer.
  4. The pointer now names the same int; it is not a second copy.
value: [42]
ptr:    points to value

Exercise: addresses.c

Create an int, store its address in an int pointer, and print the value and pointer address