5.1.8 · HinglishC Programming

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

1,809 words8 min readRead in English

5.1.8 · Coding › C Programming


1. Pointer kya hota hai? (WHAT)

Do facts jo sab kuch clear kar dete hain:

  1. Har variable kisi address par rehta hai (ek number).
  2. Pointer bas woh number store karta hai, saath mein ek type tag bhi, taaki compiler ko pata chale ki kitne bytes padhne hain aur unhe kaise interpret karna hai.

2. Do operators (WHAT + WHY)


3. Pehle principles se samajhna (HOW)

Ise byte by byte build karte hain. Maan lo:

int x = 42;
int *p = &x;

Step 1 — int x = 42; Kyun: compiler ek int ke liye jagah reserve karta hai (maan lo 4 bytes) kisi address par, e.g. 0x1000, aur wahan 42 likhta hai.

Address Contents
0x1000 42 (yeh x hai)

Step 2 — int *p = &x; Kyun: &x evaluate hoke 0x1000 deta hai. p khud bhi ek variable hai (kahin rehta hai, maan lo 0x2000) aur hum usme 0x1000 number store karte hain.

Address Contents
0x1000 42 (x)
0x2000 0x1000 (p)

Step 3 — *p Kyun: p ki value lo (0x1000), wahan jao, ek int padho → 42. Toh *p == x.

Step 4 — *p = 99; Kyun: address 0x1000 par jao aur 99 likho. Ab x bhi 99 ho gaya — kyunki p x ki taraf point kar raha hai. Yahi woh tarika hai jisse functions caller ki variables modify karte hain.

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

4. Kaam karne wale examples


5. Addresses padhna aur print karna


6. 80/20 core


7. Active recall

Recall Pehle predict karo, phir reveal karo

Kholo se pehle har answer predict karo.

  • & tumhe kya deta hai? → ek variable ka address.
  • *p tumhe kya deta hai? → p mein stored address par stored value.
  • *(&y) kya hai? → bas y.
  • swap ko pointers kyun chahiye? → C arguments copy karta hai; addresses use originals tak pahuncha dete hain.
Recall Feynman: ek 12-saal ke bachche ko explain karo

Soch ek hallway mein lockers hain. Har locker ka ek number hai aur usme ek note hoti hai. Ek pointer ek aisa locker hai jiski note sirf doosre locker ka number likhti hai. & poochta hai "is locker ka number kya hai?" * kehta hai "is number wale locker par jao aur uski note padho." Agar tum us door wale locker ki note change karte ho (*p = ...), toh jo bhi baad mein woh locker kholta hai use nayi note dikhti hai — chahe woh tumhara dost hi kyun na ho jiska woh locker hai. Isi tarah ek function doosre ki variable change karta hai.


Flashcards

What does the & operator return for a variable x?
x ka memory address, jiska type "pointer to type-of-x" hota hai.
What does dereferencing *p produce?
p mein stored address par stored value (ise assign bhi kiya ja sakta hai).
Why are * and & called inverse operators?
*(&x) evaluate hoke wapas x deta hai: & address banata hai, * usse follow karke value tak pahunchta hai.
In int *p; what role does the * play?
Yeh type declaration ka hissa hai jiska matlab hai "p ek pointer to int hai" — yeh dereference operator NAHI hai.
Why must swap(int*, int*) take pointers?
C arguments by copy pass karta hai; addresses pass karne se function caller ki actual variables modify kar sakta hai.
What does int *p, q; declare?
p ek pointer to int hai, lekin q ek plain int hai (* sirf p se bind hota hai).
Why does p + 1 for int *p skip 4 bytes (typically)?
Pointer arithmetic sizeof(int) se scale karta hai; pointer ka type step size set karta hai.
What is the danger of dereferencing a NULL or uninitialised pointer?
Yeh ek invalid address access karta hai → undefined behaviour / crash; hamesha *p se pehle check karo.
How do you safely print an address?
%p use karo aur argument ko (void*) mein cast karo, e.g. printf("%p", (void*)&x);.
Does sizeof(p) give the size of the pointer or its target?
Pointer ka khud ka size (e.g. 64-bit par 8 bytes), pointed-to object ka nahi.

Connections

  • Arrays in C — array name apne pehle element ka pointer ban jaata hai.
  • Pointer Arithmetic — kyun p + n, sizeof(*p) se scale karta hai.
  • Pass by Reference vs Pass by Value — woh reason jiske liye pointers parameters ke roop mein exist karte hain.
  • Dynamic Memory malloc free — heap memory ke pointers.
  • Strings as char pointerschar * aur string handling.
  • NULL and Segmentation Faults — invalid dereference ke 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