Lesson 19 / 36
Pointer Basics
A variable that stores a memory address.
A pointer is a house address
A normal variable is like a house holding a value. A pointer is a piece of paper with that house's address written on it — not the house itself.
& and * — address-of and dereference
&x gives the address of x. *p reads (or writes) the value at the address p holds.
int x = 10;
int *p = &x; // p holds x's address
printf("%d\n", x); // 10
printf("%d\n", *p); // 10 -- value at that address
*p = 20; // change x through p
printf("%d\n", x); // 20
Output:
10 10 20
Declared type matters
int *p means "p points to an int", not "*p is int". The * in a declaration is part of the pointer's type, telling C how many bytes to read/write through it.