Lesson 18: Pointers — addresses, & and *
Every variable in your program lives at a real memory address — just like an apartment on a street. The & operator gives the address of x (the mystery of scanf("%d", &x) from lesson 3 is finally solved!). A pointer is a variable that stores an address: int *p = &x;. With *p you access the value at t
A pointer is a note with an apartment's address. &x writes the address on the note, and *p says 'go to the address on the note and look at (or change) what's inside'. Changing through the note changes the real apartment.
- memory address
- The numeric location where a variable lives in memory. Every variable gets an address.
- & (address-of operator)
- &x returns the address of x — not its value.
- pointer
- A variable that stores the address of another variable. Declared: int *p = &x;.
- * (dereference)
- *p accesses the value at the address p holds. Read and write: *p = 99; changes x.
- NULL
- A special value meaning 'this pointer points at nothing'. Always check before use.