Pointers are how C talks about memory itself — an address is just a number that names a location.
1 A pointer is an address
A pointer is a variable whose value is the memory location of another variable.
2 & takes, * follows
&x produces the address of x; *p follows an address back to the value stored there.
3 Write through the arrow
*p = 42 changes the variable p aims at — the pointer itself just holds a location.
4 Why functions need them
C arguments are copies. Passing addresses is the only way a function can change your real variables.
Memory has addresses
A pointer is an address1A pointer is a variable whose value is the memory location of another variable. & takes, * follows2&x produces the address of x; *p follows an address back to the value stored there. Write through the arrow3*p = 42 changes the variable p aims at — the pointer itself just holds a location. Why functions need them4C arguments are copies. Passing addresses is the only way a function can change your real variables. 4 building blocks make up Memory has addresses
Tabs switch programs — watch a real swap() call, then replay the pointer mechanics step by step.
preprocessThe preprocessor pastes in stdio.h — printf comes from there.
Memory
no variables yet — press play
Console
output appears as the program runs…
The purple box is the pointer. The arrow shows which variable it currently aims at — drag to orbit.
Drag to orbit the scene. Press Play to watch memory in action.
A call pushes a frame onto the stack; the return pops it back off.
process decision input / output start / end One pointer, one write — what ends up in a?
Your predicted output — one line per console line
Hint: p holds the address of a. *p = 42 writes through that address — a itself changes.
01A pointer stores an address; & takes one, * follows one.
02*p reads or writes whatever p points at — the arrow follows the address.
03C arguments are copies — pass addresses to let a function change the originals.
04swap(&x, &y) works because *a and *b ARE x and y inside the function.
05Local variables live on the stack; their frames vanish when the function returns.