Part 3 — Chapter 14

Chapter 14: Structures

Sometimes a program needs to store different types of related data together. Using separate variables for each value becomes difficult to manage as programs grow larger. In C, structures are used to group multiple variables of different data types under a single, unified name.

Structures help organize complex data in a clean, logical, and contiguous format inside system RAM.

1. What is a Structure?

A structure is a user-defined data type that combines multiple variables of different types into a single unit. For example, a student record may contain a name (string), roll number (integer), and marks (float). All of these can be grouped inside one structure.

Structure Syntax

Syntax
C
struct structure_name
{
    data_type member1;
    data_type member2;
};
2. Memory & Code Example

When you create a structure variable, C allocates memory for its members contiguously in RAM. To access individual members, the membership dot (.) operator is used.

Example Program
student_records.c
C
#include <stdio.h>

struct Student // 1. Define a custom template (No memory is allocated yet)
{
    int roll;     // 4 Bytes: Stores roll number (Allocated at offset 0x5000)
    float marks;  // 4 Bytes: Stores marks (Allocated at offset 0x5004)
    char grade;   // 1 Byte: Stores grade letter (Allocated at offset 0x5008)
};

int main()
{
    struct Student s1; // 2. Declare s1 (Allocates contiguous memory block in RAM)

    // 3. Use membership dot operator (.) to assign values at memory offsets
    s1.roll = 101;
    s1.marks = 89.5;
    s1.grade = 'A';

    // 4. Read and print values from contiguous memory offsets
    printf("Roll Number: %d\n", s1.roll);
    printf("Marks: %.1f\n", s1.marks);
    printf("Grade: %c\n", s1.grade);

    return 0;
}
Console Output
Roll Number: 101
Marks: 89.5
Grade: A

Structure Contiguous RAM Layout

Contiguous Allocation & Address Offsets of s1
struct Student s1;
s1.roll = 101; s1.marks = 89.5; s1.grade = 'A';
Offset Address Member variable Stored Value Size / Type
0x5000 s1.roll 101 4 Bytes (int)
0x5004 s1.marks 89.5 4 Bytes (float)
0x5008 s1.grade 'A' 1 Byte (char)
0x5009 Compiler Padding [Align boundary] 3 Bytes [Padding]

Important Points

Keep these critical rules in mind when working with structures in C:

  • Contiguous Memory: All structure members are stored contiguously in memory, in the order they are declared inside the structure definition.
  • Access Operator: Structure member fields are accessed using the membership dot operator (.).
  • Templates: Defining a structure doesn't allocate memory. Memory is allocated only when a structure variable (like s1) is declared.
  • Alignment & Padding: Compilers insert padding bytes between elements to align structure members to native processor word boundaries (such as 4-byte boundaries).

Embedded Focus

Structures are heavily used in low-level embedded firmware for configuring microcontroller registers, hardware interfaces, communication protocol packets, and sensor data arrays.

sensor_config.c
C
struct SensorData
{
    int temperature;
    int pressure;
};

In high-reliability embedded firmware, using structured templates simplifies direct register mapping, handles complex serial payloads, and dramatically increases the readability and maintainability of bare-metal device drivers.

← Chapter 13: Pointers Chapter 15: Enum, Typedef and Union →