Pointer arithmetic — adding integers to pointers
5.1.9· Coding › C Programming
WHAT is pointer arithmetic?
Key idea yeh hai: pointer arithmetic type ke hisaab se scale hoti hai.
int *p;→p + 1sizeof(int)(aksar 4) bytes move karta hai.char *c;→c + 1sizeof(char)= 1 byte move karta hai.double *d;→d + 1sizeof(double)(aksar 8) bytes move karta hai.
WHY does it scale by the type? (Derivation from first principles)
Chalte hain isse derive karte hain. Maano ek array T arr[N] byte address par shuru hota hai.
Har element bytes back-to-back occupy karta hai. Toh:
Ab hum pointer p = arr define karte hain (toh p address hold karta hai). Hum demand karte hain ki *(p + i) ka matlab arr[i] ho. Yeh tab sach hoga jab p + i byte address produce kare. Isliye pointer par + ka rule yeh hona chahiye:
Isliye yeh equivalence valid hai:
HOW the compiler computes it

Forecast-then-Verify
Recall Dekhne se pehle forecast karo
int a[3]; int *p = a; Agar &a[0] ka address 0x200 hai aur sizeof(int)==4 hai, toh p + 2 kaunsa address hold karega? Aur ((char*)p) + 2 ka kya?
Verify:
p + 2→0x200 + 2*4 = 0x208(a[2]ko point karta hai).((char*)p) + 2→0x200 + 2*1 = 0x202(a[0]ke beech mein point karta hai! Yeh usually ek bug hai.)
Common mistakes (Steel-man + fix)
80/20 — woh ek rule jo sab kuch unlock kar deta hai
Feynman
Recall Ek 12-saal ke bachche ko samjhao
Socho ek hallway mein identical lockers ki ek row hai. Ek pointer yeh kehne jaisa hai ki "Main locker #5 ke saamne khada hoon." Agar main "+1" kahu, toh main ek step nahi leta — main agle locker ke saamne chala jaata hoon, woh jitna bhi wide ho. Chote lockers (chars) = ek chhoti step; mote lockers (doubles) = ek badi stride. Pointer hamesha jaanta hai ki uske lockers kitne wide hain, isliye "+1" hamesha seedha agle locker ke saamne land karta hai — kabhi do ke beech mein nahi atak ta.
Active Recall
p + 1 address mein kitne bytes add karta hai?
sizeof(*p) bytes (ek element), na ki 1 byte.arr[i] exactly kaunse pointer expression ke barabar hai?
*(arr + i) (aur *(i+arr), i[arr] bhi).int *p ke liye jab sizeof(int)==4 ho, toh p + 3 kitne bytes move karta hai?
3 * 4 = 12 bytes.Kya p + q (do pointers) legal hai?
pointer ± int aur pointer - pointer defined hain.Ek int *p ke liye ((char*)p)+1 aur p+1 mein kyun fark hai?
char* mein cast karne se stride = 1 byte ho jaati hai, toh yeh sizeof(int) ki jagah sirf 1 byte aage jaata hai.*p + 1 aur *(p+1) mein kya fark hai?
*p + 1 = p par jo value hai, plus 1. *(p+1) = agli element ki value. * ka binding + se tighter hota hai.Kya arr + N (last element ke ek baad) form karna legal hai? Kya isse dereference karna legal hai?
Pointer arithmetic ka woh general rule kya hai jo 80% questions cover karta hai?
p + n se n * sizeof(*p) bytes move hoti hain aur element n par land hota hai — "step by type, not by byte."Connections
- Arrays decay to pointers
- Pointer subtraction and ptrdiff_t
- sizeof operator
- Strings and char pointers
- Multidimensional arrays and pointers
- Undefined behavior and bounds
- Dereference operator and precedence