5.1.22 · D2 · HinglishC Programming

Visual walkthroughStructures — declaration, accessing members (. and - - )

2,251 words10 min read↑ Read in English

5.1.22 · D2 · Coding › C Programming › Structures — declaration, accessing members (. and - - )

Hum poore time ek chhota sa struct follow karenge:

struct Point { int x, y; };

Do members, x aur y, dono int. Yeh dimaag mein rakho — neeche ki har figure exactly yahi draw karti hai.


Step 1 — Memory bas numbered boxes hain

KYA: hum memory ko ek ruler of boxes ki tarah draw karte hain, addresses left se right badhte hue. KYUN: baad ka har idea (offset, pointer, dereference) yahi baat karta hai ki kaunsa box. Agar boxes dikh rahe hain, toh kuch bhi mysterious nahi. PICTURE: neeche, boxes 1000, 1001, 1002 … Har address bas ek box par ek label hai.

Figure — Structures — declaration, accessing members (. and - - )

Step 2 — Ek struct variable boxes ka ek block hai, order mein

KYA: hum a ko address 1000 se shuru karke memory mein rakhte hain. a.x boxes 1000–1003 leta hai, a.y 1004–1007 leta hai. KYUN: "order mein, ek doosre ke paas" yahi compiler ko allow karta hai ki woh member kahan hai compute kare sirf start address aur ek fixed distance se. Koi searching nahi. PICTURE: ek blue block = a. Andar, ek yellow half = x, ek green half = y.

Figure — Structures — declaration, accessing members (. and - - )
  • — woh address jahan a shuru hota hai; isse struct ka base kaho.
  • — pehla member, toh yeh base par hi baitha hai (offset ).
  • — doosra member, base se 4 bytes aage baitha hai.

Step 3 — Dot . actually "base + offset" hai

KYA: hum dikhate hain ki a.y compile hokar kya banta hai: a ka address lo, y ka offset add karo, wahan se read karo. KYUN: yahi dot ke peeche ka ek matra mechanism hai. Jab ek baar . = "base se offset bytes chalo aur pakad lo" dikh jaaye, toh arrow wahi walk hogi bas ek extra step ke saath. PICTURE: base box se ek arrow bytes hop karke y par land karta hai.

Figure — Structures — declaration, accessing members (. and - - )
  • — human-friendly naam.
  • — base address, box (& matlab "address of").
  • — member y ka offset, compile time par hamesha ke liye fixed.
  • "read at (...)" — us box par jao aur int pull karo.

Step 4 — Pointer ek box hai jo ek address store karta hai

KYA: hum memory mein kahin aur ek naya box p add karte hain (maano address 2000 par), aur usme hum number store karte hain. KYUN: real programs mein (function arguments, linked lists) aksar tumhare paas sirf yeh note hota hai, object nahi. Tumhe object ko note ke through reach karna seekhna hoga. PICTURE: 2000 par box p jo "1000" pakde hai, ek lambi arrow block a ke start tak ud ke jaati hai.

Figure — Structures — declaration, accessing members (. and - - )
  • — uska apna box (yahan 2000 par); ise a ke saath confuse mat karo.
  • p ke andar ka value: ek pointer ka value ek address hota hai.
  • — woh address wahan hota hai jahan a shuru hota hai.

Step 5 — Dereference karna *p note ko follow karke object tak pahunchta hai

KYA: hum p par * apply karte hain. Yeh p se read karta hai, box 1000 par travel karta hai, aur hum struct par hi land karte hain. KYUN: hum kisi note se member nahi le sakte. Pehle object chahiye. *p "address par chalo" wali step hai. PICTURE: p se arrow follow ki jaati hai, aur ek spotlight block a par land karta hai — hum ab object "pakde" hain, bilkul waise jaise humne a likha hota.

Figure — Structures — declaration, accessing members (. and - - )
  • — "jahan p point karta hai wahan jao."
  • — woh address jo yeh read karta hai aur travel karta hai.
  • — tum exactly wahi block par pahunchte ho jaise Step 2 mein tha. Yahan se tum Step 3 wali situation mein ho.

Step 6 — Ab bas dot karo: (*p).m

KYA: hum do moves combine karte hain — note follow karo (*p), phir offset chalo (.y). KYUN: parentheses zaroori hain kyunki . * se zyada tightly grab karta hai. Unke bina, *p.y parse hoga *(p.y) ki tarah — "pehle p ka member y lo (pointer ke paas koi member nahi!) phir dereference karo" — jo galat hai aur compile nahi hoga. PICTURE: do hops sequence mein dikhaye gaye: hop A (*p) block a tak lamba jump; hop B (.y) y tak chota jump.

Figure — Structures — declaration, accessing members (. and - - )
  • — object, parentheses forcing "pehle dereference karo."
  • — Step 3 se ordinary base+offset walk.
  • — exact box arithmetic jo result mein aata hai: address .

Step 7 — Arrow un do hops ke liye pure shorthand hai

KYA: hum clumsy (*p).y ko p->y se replace karte hain. Dono same box arithmetic mein compile hote hain: . KYUN: pointers-to-structs constantly appear hote hain (har linked-list node isi tarah reach hota hai — dekho Linked Lists). Har jagah (*p).m type karna noisy aur error-prone hai, toh C arrow bake in karta hai. PICTURE: Step 6 ka two-hop path, ek single labelled arrow -> mein collapse hota hai, dono same byte par land karte hain.

Figure — Structures — declaration, accessing members (. and - - )
  • — shortcut; padhte hain "jo p point karta hai uska y."
  • — uska exact meaning, na zyada na kam.
  • — woh single physical box jis par dono expressions end hoti hain.

Ek-picture summary

Figure — Structures — declaration, accessing members (. and - - )

Yeh final picture poori journey stack karti hai: memory as boxes → struct ek ordered block hai → dot = base+offset → pointer ek box hai jo base pakde hai → dereference use follow karta hai → phir dot → arrow do hops ko ek symbol mein collapse karta hai. Upar se neeche padho aur tumne -> ko raw bytes se re-derive kar liya.

Recall Feynman retelling — plain words mein poora walkthrough

Ek numbered mailboxes ki street imagine karo. Tumhara struct a do adjacent mailboxes hai: box 1000 mein x hai, box 1004 mein y hai. Jab tum a.y likhte ho, compiler "y" ko dhundhta nahi — use bas pata hai "y start se 4 boxes aage baitha hai," toh woh par jaata hai. Yahi dot hai: start address plus ek fixed offset. Ab ek dost tumhe ek sticky note deta hai jis par "1000" likha hai. Woh note pointer p hai; woh apne alag faraway box mein rehta hai aur sirf 1000 number store karta hai. Tum kisi sticky note se y nahi le sakte. Toh pehle tum box 1000 par chalo — woh chalna hi star hai, *p. Jab ek baar tum struct par khade ho, y pakadna wahi purana +4 dot hai: (*p).y. Do moves: chalo, phir step karo. Kyunki real code mein tum yeh hazaar baar karte, C tumhe ise ek arrow ki tarah likhne deta hai — p->y — jiska matlab exactly hai "wahan chalo, phir y tak step karo." Same mailbox, same value, bas kam keystrokes aur koi parenthesis traps nahi.

Recall

p->m kis expression ka shorthand hai? ::: (*p).m (*p).m mein parentheses kyun zaroori hain? ::: . * se tighter bind karta hai, toh *p.m parse hoga *(p.m) ki tarah. Dot . physically kya compile down hota hai? ::: read at (object ka base address + member ka fixed offset). Pointer variable actually kya store karta hai? ::: ek address — doosre box ka number, object khud nahi. *p kaunsi single step perform karta hai pehle member reachable ho? ::: yeh stored address ko follow karta hai object par khade hone ke liye.


Connections

  • Structures — parent topic — yeh page uske -> rule ki visual derivation hai.
  • Pointers in C — Steps 4–5 (ek box mein stored address, dereferencing) pointer fundamentals hain.
  • Linked Lists — arrow ka real ghar: har node node->next se reach hota hai.
  • Memory Alignment & Padding — kyun offsets hamesha naive sum nahi hote (Step 3 ka "+4" jump kar sakta hai).
  • Passing Structures to Functions — kyun tum &a pass karte ho aur -> se andar jaate ho.