Pointers 101
A pointer is an ordinary variable. The value it stores happens to be an address.
Before you start
A **pointer** sounds mystical, but it is the most ordinary thing possible: a variable whose value happens to be a memory address rather than a number or a character.
Why does this exist? Because sometimes you need to say "the thing over *there*" instead of "a copy of the thing". You want to modify what another function has, share one big object between many places, or build a list that can grow.
This lesson shows the two operators that make pointers work — & ("the address of") and * ("the value at") — and watches an arrow appear between two boxes in memory. Once you see that arrow, pointers stop being mystical.
Stack
Step 1 of 8. Start with something to point at. `x` is a normal `int`: 4 bytes in `main`'s frame, holding 42, living at `0x7ffd4c10`. Nothing about `x` is special. Any variable with an address — which is all of them — can be pointed at.
Step 01 / 08
Start with something to point at
x is a normal int: 4 bytes in main's frame, holding 42, living at 0x7ffd4c10.
Nothing about x is special. Any variable with an address — which is all of them — can be pointed at.
#include <stdio.h>int main(void) { int x = 42; int *p; p = &x; printf("%d\n", *p); *p = 99; p = NULL; return 0;}