Pointers — declaration, dereferencing ( - ), address-of (&)
1. What is a pointer? (WHAT)
Two facts that make everything click:
- Every variable lives at some address (a number).
- A pointer just stores that number, plus a type tag so the compiler knows how many bytes to read and how to interpret them.
2. The two operators (WHAT + WHY)
3. Deriving how it works from first principles (HOW)
Let's build it byte by byte. Suppose:
int x = 42;
int *p = &x;Step 1 — int x = 42;
Why: the compiler reserves space for one int (say 4 bytes) at some address, e.g. 0x1000, and writes 42 there.
| Address | Contents |
|---|---|
| 0x1000 | 42 (this is x) |
Step 2 — int *p = &x;
Why: &x evaluates to 0x1000. p is itself a variable (lives somewhere, say 0x2000) and we store the number 0x1000 inside it.
| Address | Contents |
|---|---|
| 0x1000 | 42 (x) |
| 0x2000 | 0x1000 (p) |
Step 3 — *p
Why: take the value in p (0x1000), go there, read an int → 42. So *p == x.
Step 4 — *p = 99;
Why: go to address 0x1000 and write 99. Now x is 99 too — because p is pointing at x. This is exactly how functions modify caller variables.

4. Worked examples
5. Reading & printing addresses
6. The 80/20 core
7. Active recall
Recall Forecast first, then reveal
Predict each answer before opening.
- What does
&give you? → the address of a variable. - What does
*pgive you? → the value stored at the address inp. - What is
*(&y)? → justy. - Why does
swapneed pointers? → C copies arguments; addresses let it reach the originals.
Recall Feynman: explain to a 12-year-old
Imagine lockers in a hallway. Each locker has a number and holds a note. A pointer is a locker whose note just says another locker's number. & asks "what number is this locker?" * says "go to the locker this number names and read its note." If you change the note at that far locker (*p = ...), then anyone who opens that locker later sees the new note — even your friend who owns it. That's how one function changes another's variable.
Flashcards
What does the & operator return for a variable x?
x, with type "pointer to type-of-x".What does dereferencing *p produce?
p (it can also be assigned to).Why are * and & called inverse operators?
*(&x) evaluates back to x: & makes an address, * follows it back to the value.In int *p; what role does the * play?
Why must swap(int*, int*) take pointers?
What does int *p, q; declare?
p is a pointer to int, but q is a plain int (the * binds only to p).Why does p + 1 for int *p skip 4 bytes (typically)?
sizeof(int); the pointer's type sets the step size.What is the danger of dereferencing a NULL or uninitialised pointer?
*p.How do you safely print an address?
%p with the argument cast to (void*), e.g. printf("%p", (void*)&x);.Does sizeof(p) give the size of the pointer or its target?
Connections
- Arrays in C — an array name decays to a pointer to its first element.
- Pointer Arithmetic — why
p + nscales bysizeof(*p). - Pass by Reference vs Pass by Value — the reason pointers exist as parameters.
- Dynamic Memory malloc free — pointers to heap memory.
- Strings as char pointers —
char *and string handling. - NULL and Segmentation Faults — invalid dereference consequences.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, pointer ka idea bilkul simple hai. Memory ko ek lambi street samjho jisme har ghar ka ek number (address) hai. Normal variable matlab ghar ke andar rakha hua saaman. Aur pointer ek aisa ghar hai jo kisi doosre ghar ka number store karta hai. Bas itni si baat hai. &x poochta hai "x ka ghar number kya hai?", aur *p kehta hai "jo number p ke andar likha hai, us ghar pe jao aur andar dekho."
Yeh dono operators ek doosre ke ulte hain — *(&x) ka matlab seedha x hi hota hai. & se address milta hai, * se us address pe ja kar value milti hai. Yaad rakhne ka trick: "& gives the home, * goes home." Round trip ho gaya, wapas wahi value.
Pointers kyun zaroori hain? Kyunki C me jab tum function ko variable dete ho, woh sirf ek copy bhejta hai. Isliye agar tumhe caller ka original variable badalna hai (jaise swap me), to value nahi, address bhejo. Function *a = ... karke seedha original ghar me likh deta hai. Yahi hai pass-by-reference ka pura jugaad.
Ek important warning: kabhi bhi NULL ya uninitialised pointer ko dereference mat karo — woh "kahin nahi" point karta hai aur program crash ho jaayega. Hamesha if (p != NULL) check karo. Aur dhyan rakho: int *p, q; me sirf p pointer hai, q to normal int hai. Yeh chhoti galti exam aur interview dono me bahut puchi jaati hai!