NBC 103 • Unit 416 min readHigh Exam Frequency

Structures (struct) & Architecture Memory Alignment

Unit 4: Structures, Unions, Enums & Storage ClassesProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Multi-Compartment Bento Box vs Uniform Egg Carton

Array ek uniform ande ki tray (egg carton) jaisa hai jisme har slot bilkul ek jaisa hota hai (sirf int ya float). Lekin real life me agar ek Student ka record banana ho, toh usme Roll No (int), Name (string), aur Marks (float) sab ek sath chahiye! Yahan kaam aata hai Structure (struct)—ek multi-compartment bento lunch box ki tarah jisme roti, sabzi, aur mithai alag-alag aakaar ke hote hue bhi ek hi box ke andar safely pack rehte hain!

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. Structure Definition, Template & Variable Declaration

Definition: A Structure is a user-defined composite data type that groups logically related, heterogeneous (differing data types) variables under a single common identifier.
Syntax Template:

struct Student {

int roll_no; // 4 Bytes

char name[30]; // 30 Bytes

float marks; // 4 Bytes

};

Declaring Structure Variables:
- Method 1 (Tagged): `struct Student s1, s2;`
- Method 2 (Along with definition): `struct Student { ... } s1, s2;`
Initializing Structures:

`struct Student s1 = {101, "Rahul Kumar", 88.5f};`

Direct member assignment uses the Dot Operator (`.`): `s1.roll_no = 102;`.

NOTEBOOK PAGE 2 OF 3

2. The Dot Operator (.) vs Member Selection

Direct Member Access: The period symbol (`.`) is the Dot Operator or Direct Member Selector.
Syntax: `structure_variable_name.member_name`
Dot operator has high precedence (Rank 1 alongside parentheses `()`), meaning `s1.marks + 5` accesses `marks` first, then adds 5.
NOTEBOOK PAGE 3 OF 3

3. Structure Padding & Memory Alignment Rules

Why does `sizeof(struct Demo)` not always equal the mathematical sum of its members?

CPU Word Boundary Alignment:

Modern 32-bit and 64-bit processors access memory in 4-byte or 8-byte 'words'. To accelerate data transfers, the compiler aligns variables at memory addresses that are multiples of their size.

Structure Padding (Slack Bytes):

Consider:

struct Example {

char c; // 1 byte

// 3 padding bytes inserted by compiler!

int i; // 4 bytes (must start at a 4-byte boundary)

};

Total size = 1 + 3 (padding) + 4 = 8 Bytes (NOT 5 bytes)!

Packing Directive:

To prevent padding and optimize for network packets or embedded storage, use `#pragma pack(1)` to force contiguous single-byte byte packing.

Memory Alignment & Structure Padding Byte Map (struct Example)
Byte OffsetAllocated MemberByte SizePurpose & Architectural Reason
Byte 0char c1 ByteStores char data
Bytes 1, 2, 3[PAD] Slack Bytes3 BytesInserted by compiler to align next integer to 4-byte boundary
Bytes 4, 5, 6, 7int i4 BytesAligned on 4-byte word boundary for ultra-fast CPU bus fetching
Total Sizesizeof(struct Example)8 BytesCalculated structure size including padding

Interactive Tested Code Example

#include <stdio.h>

struct Student {
    int roll_no;
    char name[20];
    float marks;
};

struct PackedDemo {
    char a;
    int b;
};

int main(void) {
    struct Student s1 = {101, "Aman Verma", 91.5f};

    printf("=== STUDENT STRUCTURE RECORD ===\n");
    printf("Roll No: %d\n", s1.roll_no);
    printf("Name:    %s\n", s1.name);
    printf("Marks:   %.1f%%\n\n", s1.marks);

    printf("=== STRUCTURE PADDING DEMONSTRATION ===\n");
    printf("sizeof(char): %zu | sizeof(int): %zu\n", sizeof(char), sizeof(int));
    printf("Mathematical Sum: 1 + 4 = 5 Bytes\n");
    printf("Actual sizeof(struct PackedDemo): %zu Bytes (Includes 3 Slack Padding Bytes)\n", sizeof(struct PackedDemo));
    return 0;
}
💡 Note:Demonstrates structure member initialization, access via dot operator (.), and practical validation of structure padding using sizeof.

🎯 University Exam Scoring Blueprint

  • In university exams, always explain Structure Padding with a byte-map diagram showing the slack bytes.
  • Explain that the struct tag by itself does not allocate memory; memory is allocated only when structure variables are declared.
  • Mention that structure variables of the same type can be copied directly using simple assignment: s2 = s1;.

Top Viva Questions on Structures (struct) & Architecture Memory Alignment

2 Questions
1

Can two different structure types contain members with identical names?

2

What is the purpose of #pragma pack(1)?