5.1.22 · D1 · HinglishC Programming

FoundationsStructures — declaration, accessing members (. and - - )

1,938 words9 min read↑ Read in English

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

Parent note ki ek bhi line padhne se pehle, tumhare paas pehle se kuch symbols aur words ka "ownership" hona chahiye. Parent note quietly assume karta hai ki tum unhe jaante ho. Yeh page unhe ek-ek karke zero se build karta hai, ek aisi order mein jahan har ek iint uske neeche wali iint pe tikti hai.


0. Shuruaati picture: memory mein ek labelled slot

Figure dekho: pale-yellow box address 1000 pe ek ghar hai. Humne use age nickname diya, aur andar value 19 rehti hai. Naam ek human label hai; address woh jagah hai jahan woh actually hai. Apne dimag mein dono ko alag rakho — . vs -> ka poora distinction isi se paida hota hai.


1. Type — "box mein kis shape ki cheez fit hoti hai"

Topic ko yeh kyun chahiye? Kyunki structure ka poora selling point types ko mix karna hai — ek naam (char[20]), ek roll number (int), aur marks (float) ek package mein. Agar har value ko same type hona padta, tum sirf ek array use karte. Toh "type" pehla idea hai jise humein samajhna hoga.

Recall Quick check: type kya decide karta hai?

Box kitna bada hai (bytes) aur stored bits ko kaise interpret karna hai ::: haan — size + meaning.


2. Array — "ek row mein same-type ke kai boxes, number se pahunche"

Figure mein name[20] ek row mein 20 chalk-blue cells dikhata hai. Tum ek ko number se point karte ho: name[3]. Yeh structure ka cousin hai — Arrays vs Structures dekho. Comparison:

Array Structure
element types sab same alag-alag ho sakte hain
kaise select karte hain ek number [i] se ek naam .member se

Parent ka char name[20] member ek aisa array hai jo structure ke andar rehta hai — isliye tum strcpy(s.name, "Asha") likhte ho na ki s.name = "Asha" (tum poore array ko naam se reassign nahi kar sakte).


3. Address-of operator & — "mujhe ghar ka number do"

Topic ko & kyun chahiye? Kyunki parent ke Example 2 mein grow(&a) call hota hai — woh a ka address pass karta hai taaki function wapas pahunche aur original ko edit kar sake. & ke bina, function sirf ek copy paata aur uske edits gayab ho jaate.


4. Pointer aur dereference * — "ek note jo batata hai kahan"

Figure mein, pink box p address 2000 pe hai lekin number 1000 store karta hai — aur p se 1000 pe box tak ek arrow drawn hai. Woh drawn arrow ki wajah se operator literally -> likha jaata hai.

Toh & aur * opposites hain:

x ka address lo (&x), phir usse wapas follow karo (*), aur tum x pe land karte ho — tum gaye aur wapas aaye.

Yeh parent ka use kiya hua sabse important derivation hai — aur ab isme har symbol (*, ., parentheses, arrow) zero se earn kiya gaya hai.


5. Dot . — "YEH box kholo, WOH compartment lo"

Figure ek Student box dikhata hai jisme teen stacked compartments hain — name offset 0 pe, roll thoda aage, marks aur aage. Dot ek aise box mein tumhara haath dalna hai jo pehle se tumhare haath mein hai. Koi travelling nahi, koi arrow nahi.


6. Braces { } aur semicolon ; — "box ka blueprint yahan khatam hota hai"


Prerequisite map

Memory as numbered houses

Variable = named box

Type = size plus meaning

Array = same-type boxes by number

Structure = mixed-type boxes by name

Address-of and operator

Pointer stores an address

Dereference star follows the note

Dot picks a compartment

Arrow equals deref then dot

Structures accessing members

Ise bottom-up padho: houses → variables → types → (arrays + structs) → address-of → pointers → dereference → arrow, sab parent topic ko feed karte hain. Pointer wale half ke liye Pointers in C bhi dekho aur jahan -> unavoidable ho jaata hai uske liye Linked Lists dekho.


Equipment checklist

Khud test karo — tum parent note ke liye ready ho sirf tab jab yeh sab instantly aaye.

Memory kya hai, ek picture mein?
Numbered houses (bytes) ki lambi street; har address ek house number hai.
Variable kya hai?
Memory mein ek named box jo ek type ki ek value rakhta hai.
Ek value ke baare mein ek type kya decide karta hai?
Bytes mein uski size aur uske bits ko kaise interpret karna hai.
Array ek structure se kaise alag hai?
Array = same type, number index se chunna; structure = mixed types, naam se chunna.
&x ka matlab kya hai?
x ka address (house number).
Pointer kya hai?
Ek variable jiska value ek address hai — ek note jo batata hai doosra box kahan hai.
Expression mein *p kya karta hai?
Pointer follow karta hai — p mein address pe jaata hai aur wahan jo hai woh read karta hai.
*(&x) kis ke barabar hai, aur kyun?
x ke — address lo phir usse wapas follow karo ghar.
(*p).m ko parentheses kyun chahiye?
. * se zyada tight bind karta hai; parens ke bina *p.m ka matlab *(p.m) hoga.
p->m kis cheez ka shorthand hai?
(*p).m — pointer follow karo, phir member m chuno.
. vs -> kab use karte ho?
. jab object haath mein ho; -> jab uski taraf pointer haath mein ho.
struct {...} ; se kyun khatam hona chahiye?
Yeh ek declaration statement hai, aur sab declarations ; se khatam hoti hain.

Connections

  • Pointers in C&, *, aur arrow machinery yahan fully rehti hai.
  • Arrays vs Structures — same-type-by-number vs mixed-type-by-name.
  • Linked Lists — pehli jagah jahan -> zaroori ho jaata hai.
  • typedef — type naam karte waqt struct word chhod ne deta hai.
  • Memory Alignment & Padding — kyun offsets mein gaps hote hain.
  • Passing Structures to Functions — editing ke liye &a kyun matter karta hai.