Structures — declaration, accessing members (. and - - )
WHAT is a structure?
struct Student { // 'Student' is the structure TAG (the type's name)
char name[20]; // member 1
int roll; // member 2
float marks; // member 3
}; // <-- semicolon REQUIRED hereHOW memory is laid out

Creating variables & accessing members
The dot operator . — for a struct value/variable
struct Student s; // s is an actual Student object
strcpy(s.name, "Asha"); // can't do s.name = "Asha"; (it's an array)
s.roll = 42;
s.marks = 91.5;
printf("%s %d %.1f", s.name, s.roll, s.marks);The arrow operator -> — for a pointer to a struct
struct Student s = {"Asha", 42, 91.5};
struct Student *p = &s; // p points at s
printf("%d", (*p).roll); // = 42 (the long way)
printf("%d", p->roll); // = 42 (identical, the arrow way)
p->marks = 99.0; // modifies s.marks directlyWorked examples
Recall Feynman: explain it to a 12-year-old
Imagine a lunchbox with 3 compartments: rice, dal, sweet. The lunchbox is a structure. Naming a compartment with a dot — "lunchbox**.**sweet" — means "open this exact box, grab the sweet."
Now your friend gives you only a note saying where your box is kept (that's a pointer). You can't grab the sweet from a note! So the arrow note->sweet means "go to the place the note points to, then grab the sweet." Same food, just one extra step of walking there first.
Flashcards
What keyword declares a structure in C?
structWhy must a struct definition end with a semicolon?
;.Can a structure hold members of different types?
char[], int, float together).When do you use the dot operator .?
When do you use the arrow operator ->?
p->m is exactly equivalent to what longer expression?
(*p).mWhy are the parentheses needed in (*p).m?
. binds tighter than *, so *p.m would wrongly parse as *(p.m).Why pass &a instead of a to a function that must modify it?
a copies it; passing the address &a lets the function edit the caller's actual object.How do you access field d of inner struct when inside event value e?
e.when.dWhat's the rule of thumb for choosing . vs ->?
.; hold its address (pointer) → ->.Connections
- Pointers in C —
->only exists because of pointers-to-structs. - Arrays vs Structures — arrays = same type, indexed; structs = mixed types, named.
- Linked Lists — every node is a struct accessed via
->. - typedef — lets you drop the word
structwhen naming the type. - Memory Alignment & Padding — why
sizeof(struct)can exceed the sum of members. - Passing Structures to Functions — by value (copy) vs by pointer (efficient, mutable).
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, structure ka idea simple hai: ek real cheez ke alag-alag parts ko ek hi naam me bundle karna. Jaise ek student ka name, roll, aur marks — teeno milke ek student banate hain. Agar inko alag-alag variables me rakho to galti se mix ho sakte hain. struct Student { char name[20]; int roll; float marks; }; likhke hum apna khud ka data type bana lete hain. Yaad rakho — closing } ke baad semicolon lagana zaroori hai, warna compiler agle line pe ajeeb error dega.
Ab members ko access karne ke do tareeke hain. Agar tumhare paas object khud hai (jaise struct Student s;), to dot use karo: s.roll. Lekin agar tumhare paas object ka address yaani pointer hai (jaise struct Student *p = &s;), to seedha dot nahi chalega — pehle pointer ko follow karna padta hai. Iske liye arrow -> hai: p->roll. Yeh actually (*p).roll ka short form hai — *p se object milta hai, fir .roll se member.
Yaad rakhne ka trick: "Arrow door ishaara karta hai — wahan tak chal ke jaana padta hai." Pointer matlab kahin aur, to arrow. Object haath me hai, to bas dot. Yeh -> ka pura faayda linked list aur functions me dikhta hai — jab tum &a pass karte ho taaki function caller ka actual data badal sake, tab andar -> se hi kaam hota hai. Bina pointer ke function sirf copy badalta, original safe reh jaata.