5.1.8C Programming

Pointers — declaration, dereferencing ( - ), address-of (&)

1,969 words9 min readdifficulty · medium6 backlinks

1. What is a pointer? (WHAT)

Two facts that make everything click:

  1. Every variable lives at some address (a number).
  2. 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 int42. 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.

Figure — Pointers — declaration, dereferencing ( - ), address-of (&)

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 *p give you? → the value stored at the address in p.
  • What is *(&y)? → just y.
  • Why does swap need 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?
The memory address of x, with type "pointer to type-of-x".
What does dereferencing *p produce?
The value stored at the address held in 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?
It's part of the type declaration meaning "p is a pointer to int" — NOT the dereference operator.
Why must swap(int*, int*) take pointers?
C passes arguments by copy; passing addresses lets the function modify the caller's actual variables.
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)?
Pointer arithmetic scales by sizeof(int); the pointer's type sets the step size.
What is the danger of dereferencing a NULL or uninitialised pointer?
It accesses an invalid address → undefined behaviour / crash; always check before *p.
How do you safely print an address?
Use %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?
The size of the pointer itself (e.g. 8 bytes on 64-bit), not the pointed-to object.

Connections

  • Arrays in C — an array name decays to a pointer to its first element.
  • Pointer Arithmetic — why p + n scales by sizeof(*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 pointerschar * and string handling.
  • NULL and Segmentation Faults — invalid dereference consequences.

Concept Map

lives at

stores

has

tells compiler

turns var into

turns address into

inverse of

can be assigned

modifies

passed to function

enables

star is part of

star is operator

Variable stores value

Memory address

Pointer variable

Type tag int star

How many bytes to read

Address-of &

Dereference *

Write to target

Function modifies caller var

swap that works

Declaration int *p

Expression *p

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!

Go deeper — visual, from zero

Test yourself — C Programming

Connections