Exercises — Structures — declaration, accessing members (. and - - )
5.1.22 · D4· Coding › C Programming › Structures — declaration, accessing members (. and - - )
Neeche wali picture is poore page ka ek hi mental model hai — jab bhi confuse ho ki kaunsa operator use karein, ise dekh lo.

Level 1 — Recognition
Goal: ek naam dekhte hi turant decide karo ki yeh object hai ya address, aur sahi operator choose karo.
L1.1 Tumhare paas struct Point p; hai. Member x padhne ke liye kaunsa sahi hai: p.x ya p->x?
Recall Solution L1.1
p ek plain variable hai — tumhare paas object hai. Object → dot.
Answer: p.x. p->x likhne par "invalid type argument of '->' (have 'struct Point')" error aayega kyunki -> ko ek pointer chahiye.
L1.2 Tumhare paas struct Point *q; hai (ek pointer). Member y padhne ke liye kaunsa sahi hai: q.y ya q->y?
Recall Solution L1.2
q ek address hold karta hai, object nahi. Address → arrow.
Answer: q->y. q.y likhne par "request for member 'y' in something not a structure" error aayega.
L1.3 (*q).y ko arrow operator se rewrite karo.
Recall Solution L1.3
Definition ke hisaab se .
Answer: q->y. Same matlab, koi parentheses nahi chahiye.
Level 2 — Application
Goal: chhote code mein sahi member access likho, including pointer ke through.
L2.1 struct Point { int x, y; }; declare karo, ek variable a banao, aur a.x = 5, a.y = 9 set karo. Phir a ka ek pointer p banao aur x ko pointer ke through print karo. x print karne wali line kya hai?
Recall Solution L2.1
struct Point { int x, y; };
struct Point a;
a.x = 5; // a is object → dot
a.y = 9;
struct Point *p = &a; // p holds the ADDRESS of a
printf("%d", p->x); // p is pointer → arrowPrints 5. p->x a tak jaata hai phir x utha leta hai.
L2.2 Neeche diye code ko dekho, yeh kya print karega?
struct Point { int x, y; };
struct Point a = {3, 4};
struct Point *p = &a;
p->x = 10;
printf("%d %d", a.x, p->y);Recall Solution L2.2
p usi object a ko point karta hai (kyunki &a use kiya). To p->x = 10 actually a.x ko hi edit karta hai.
a.xab10hai.p->ynea.ypadha, jo abhi bhi4hai. Prints10 4.
L2.3 s.name ek char[20] hai. s.name = "Asha"; kyun galat hai, aur tumhe kya likhna chahiye?
Recall Solution L2.3
Array name assignable nahi hota — tum = se poore array ko overwrite nahi kar sakte. Tumhe characters andar copy karne padte hain:
strcpy(s.name, "Asha");Answer: strcpy(s.name, "Asha"); use karo (iske liye #include <string.h> chahiye).
Level 3 — Analysis
Goal: pointers, nested structs, aur copy-vs-alias behaviour ko trace karo.
L3.1 Nested access. Diya gaya hai:
struct Date { int d, m, y; };
struct Event { char title[10]; struct Date when; };
struct Event e;
struct Event *pe = &e;Year y ka expression likho: (a) e use karke, (b) pe use karke.
Recall Solution L3.1
(a) e ek object hai → when mein dot (jo bhi ek object hai) → y mein dot:
e.when.y
(b) pe ek pointer hai → .when tak pahunchne ke liye arrow, jo ek value hai, phir dot:
pe->when.y
Rule: pehla hop e vs pe par depend karta hai; ek baar jab tum inner value when par land kar lo, har aage ka hop dot hoga.
L3.2 Copy vs alias. Yeh kya print karta hai?
struct Point { int x, y; };
struct Point a = {1, 1};
struct Point b = a; // copy
struct Point *p = &a; // alias
b.x = 100;
p->y = 200;
printf("%d %d", a.x, a.y);Recall Solution L3.2
b = aek independent copy banata hai.b.xedit karna kabhiako touch nahi karta.p = &aek alias banata hai — same object.p->y = 200a.yko edit karta hai. Toa.x1rehta hai,a.y200ho jaata hai. Prints1 200.
L3.3 Precedence puzzle. p ek struct Point * hai. Kya *p.x compile hoga, aur agar haan, to iska kya matlab hai?
Recall Solution L3.3
. * se zyada tight bind karta hai, to compiler ise *(p.x) padta hai. Lekin p ek pointer hai, isliye p.x pehle se hi illegal hai (non-struct par member access). Yeh compile nahi hoga.
Dereference-then-access ke liye tumhe zaroor (*p).x likhna hoga, ya simply p->x. Yeh precedence rule hi exactly wajah hai ki arrow operator exist karta hai.
Level 4 — Synthesis
Goal: chhote functions banao jo operator sahi choose karen tabhi kaam kare.
L4.1 void grow(struct Point *q) likho jo caller ke point ke dono fields mein 1 add kare. Phir a = {3,4} ke liye grow(&a) trace karo.
Recall Solution L4.1
void grow(struct Point *q) {
q->x += 1; // q is a pointer → arrow
q->y += 1;
}
struct Point a = {3, 4};
grow(&a); // pass the ADDRESS so changes stickTrace: q a ko point karta hai. q->x += 1 → a.x 4 ho jaata hai. q->y += 1 → a.y 5 ho jaata hai.
Call ke baad, a hai (4, 5).
Agar void grow(struct Point q) (by value) likhte, to grow ek copy edit karta aur a (3,4) rehta.
L4.2 Ek function int distSq(struct Point p1, struct Point p2) likho jo squared distance return kare. (0,0) aur (3,4) ke liye evaluate karo.
Recall Solution L4.2
Yahan hum points by value lete hain — hum sirf unhe read kar rahe hain, to koi pointers nahi chahiye.
int distSq(struct Point p1, struct Point p2) {
int dx = p2.x - p1.x; // dot: p1, p2 are values
int dy = p2.y - p1.y;
return dx*dx + dy*dy;
}(0,0) aur (3,4) ke liye: , to .
Returns 25. (Hum squared distance compute karte hain integers mein rehne ke liye aur sqrt se bachne ke liye.)

Level 5 — Mastery
Goal: structs, pointers, nesting, aur copy semantics ko ek reasoning chain mein combine karo — linked-list wala flavour.
L5.1 Diya gaya hai:
struct Node { int val; struct Node *next; };
struct Node a = {10, NULL};
struct Node b = {20, NULL};
a.next = &b;b ki value padhne ka expression likho a se shuru karke. Uski value kya hai?
Recall Solution L5.1
a ek object hai → next tak dot. a.next ek pointer hai (jo b ko point karta hai) → val tak pahunchne ke liye arrow:
a.next->val, jo hai 20.
Yahi exactly woh pattern hai jisse tum linked list walk karte ho: har ->next agle node par hop karta hai.
L5.2 Full trace. Yeh kya print karta hai?
struct Node { int val; struct Node *next; };
struct Node a = {10, NULL};
struct Node b = {20, NULL};
struct Node c = {30, NULL};
a.next = &b;
b.next = &c;
struct Node *p = &a;
int sum = 0;
while (p != NULL) {
sum += p->val;
p = p->next;
}
printf("%d", sum);Recall Solution L5.2
Chain a → b → c → NULL walk karo:
p = &a:sum = 0 + 10 = 10, phirp = a.next = &b.p = &b:sum = 10 + 20 = 30, phirp = b.next = &c.p = &c:sum = 30 + 30 = 60, phirp = c.next = NULL.p == NULL→ loop khatam. Prints60.
L5.3 Design + reason. Tumhare paas ek function void increment(struct Node *n) hai jo n->val += 5 karna chahta hai. Ek student ise increment(a.next) se call karta hai. L5.1 ka setup diya hai (a.next = &b, b.val = 20), call ke baad b.val kya hoga, aur yeh sirf a.next se kaam kyun karta hai (bina & ke)?
Recall Solution L5.3
void increment(struct Node *n) { n->val += 5; }
increment(a.next); // a.next is ALREADY a pointer (= &b)a.next khud hi ek address hai (isme &b stored hai), to yeh already struct Node * parameter se match karta hai — koi extra & nahi chahiye. Andar, n b ko alias karta hai, isliye n->val += 5 b ko edit karta hai.
Call ke baad, b.val = 25.
Compare karo: object a pass karne ke liye increment(&a) likhte; ek already existing pointer field pass karne ke liye use as-is pass karte hain.
Recall Jaane se pehle ek ek-line self-test
X diya gaya hai, kaunsa operator? X ek pointer hai ::: -> use karo
X diya gaya hai, kaunsa operator? X ek plain variable/value hai ::: . use karo
a.next->val — pehle dot phir arrow kyun? ::: a ek value hai (dot), a.next ek pointer hai (arrow)
Caller ke struct ko function se modify karna — kya pass karo? ::: address &a, parameter hai struct Node *
Do structs ke beech b = a kya karta hai? ::: ek independent value copy, koi link nahi
Connections
- Pointers in C — upar har
->aur har&apointer basics par tikee hai. - Linked Lists — L5 core node-walking pattern hai.
- Passing Structures to Functions — by-value vs by-pointer choice jo L4 aur L5 ke peeche hai.
- Arrays vs Structures — kyun
s.name = "..."fail hota hai (array member) lekins.roll = 42kaam karta hai. - typedef —
structword baar baar likhne se bachata hai. - Memory Alignment & Padding — kyun upar wali figure ke byte offsets mein gaps ho sakte hain.