NBC 103 • Unit 416 min readHigh Exam Frequency

Arrays of Structures, Nested Structs & Arrow Operator (->)

Unit 4: Structures, Unions, Enums & Storage ClassesProblem 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

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. Array of Structures: Managing Database Records

Purpose: Storing multiple records of identical structure format (e.g., student roster, inventory catalog).
Syntax: `struct Student class_bca[60];`
Accessing Member: `class_bca[i].roll_no` or `class_bca[i].marks`
Memory Layout: Stored contiguously in RAM. Address of record $i$ = $\text{Base} + i \times \text{sizeof(struct Student)}$.
NOTEBOOK PAGE 2 OF 3

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 };

Member Access via Chained Dot Operators: `emp1.doj.year = 2024;`
NOTEBOOK PAGE 3 OF 3

3. Structure Pointers & The Arrow Operator (->)

Pointer to Structure: `struct Student s1 = {101, "Priya", 95.0}; struct Student *ptr = &s1;`
Member Access via Pointer:

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`.

Passing Structures to Functions: Pass by Value vs Pass by Reference
ParameterPass by Value (func(struct S s))Pass by Reference (func(struct S *s))
Data TransferredComplete bitwise duplicate copy of entire structureOnly the 4-byte or 8-byte memory address is passed
RAM & Stack OverheadVery high: Copies all members (can be hundreds of bytes)Minimal: Pushes single pointer to Call Stack
ModificationsChanges inside function do NOT affect caller's recordDirectly mutates original caller's structure via pointer
Execution SpeedSlower due to memory copying overheadUltra-fast: Immediate pointer access
Member Access Syntaxs.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;
}
💡 Note:Demonstrates array-like records, nested date structures (s1.dob.day), and pointer modification using the arrow operator (->).

🎯 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.

Top Viva Questions on Arrays of Structures, Nested Structs & Arrow Operator (->)

2 Questions
1

What is the syntactic difference between (*ptr).member and *ptr.member?

2

Can a structure contain an instance of itself as a member?