Arrays of Structures, Nested Structs & Arrow Operator (->)
Unit 4: Structures, Unions, Enums & Storage Classes • Problem Solving Using C
👨🏫 Professor's Mental Model: The School Filing Cabinet & The Pointer Badge Card
Array of Structures ek school ke filing cabinet jaisa hai jisme 60 students ke complete files (records) ek ke baad ek rakhe hain: file[0], file[1], etc. Nested Structure ek file ke andar choti file rakhne jaisa hai (jaise Student file ke andar Date of Birth ki file). Aur Arrow Operator (->) ek aisi chabi hai jiske paas student file ka address hai: address par jao aur direct member bahar nikal lo!
Academic Lecture Notes & Solved Study Pages
Unit 4 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Array of Structures: Managing Database Records
2. Nested Structures (Composition / Has-A Relationship)
Embedding one structure inside another structure:
struct Date { int day; int month; int year; };
struct Employee { int emp_id; char name[30]; struct Date doj; // Nested structure member };
3. Structure Pointers & The Arrow Operator (->)
1. Dereference with Dot: `(*ptr).roll_no` (Parentheses are STRICTLY MANDATORY because dot `.` has higher precedence than `*`! Writing `*ptr.roll_no` is a syntax error).
2. The Arrow Operator (`->`): Syntactic sugar providing clean syntax: `ptr->roll_no` is identical to `(*ptr).roll_no`.
| Parameter | Pass by Value (func(struct S s)) | Pass by Reference (func(struct S *s)) |
|---|---|---|
| Data Transferred | Complete bitwise duplicate copy of entire structure | Only the 4-byte or 8-byte memory address is passed |
| RAM & Stack Overhead | Very high: Copies all members (can be hundreds of bytes) | Minimal: Pushes single pointer to Call Stack |
| Modifications | Changes inside function do NOT affect caller's record | Directly mutates original caller's structure via pointer |
| Execution Speed | Slower due to memory copying overhead | Ultra-fast: Immediate pointer access |
| Member Access Syntax | s.member (Dot operator) | s->member (Arrow operator) |
Interactive Tested Code Example
#include <stdio.h>
struct Date {
int day, month, year;
};
struct Student {
int roll_no;
char name[30];
struct Date dob; // Nested Struct
float marks;
};
// Function receiving structure pointer (Call by Reference)
void update_marks(struct Student *s, float new_marks) {
s->marks = new_marks; // Arrow operator shorthand for (*s).marks
}
int main(void) {
struct Student s1 = {105, "Kavita Rao", {15, 8, 2004}, 82.0f};
struct Student *ptr = &s1;
printf("=== NESTED STRUCTURE & POINTER ACCESS ===\n");
printf("Roll: %d | Name: %s\n", ptr->roll_no, ptr->name);
printf("DOB: %02d-%02d-%d\n", s1.dob.day, s1.dob.month, s1.dob.year);
printf("Original Marks: %.1f%%\n", s1.marks);
update_marks(&s1, 94.5f);
printf("Updated Marks via Arrow Operator: %.1f%%\n", ptr->marks);
return 0;
}🎯 University Exam Scoring Blueprint
- In exam questions, always emphasize why (*ptr).member requires parentheses: dot (.) has higher precedence than asterisk (*).
- Contrast pass-by-value vs pass-by-reference for large structures in terms of call stack memory consumption.
- Demonstrate nested structure syntax with chained dot operators: emp.doj.year.