C Arrays of Structures

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Arrays of Structures?

An array of structures holds a table of records, such as products or students. Access one record with items[i], and a field with items[i].price. Initialize with nested braces and loop to process every record.

Remember
struct Item items[3] = {
    {"Pen", 1.5, 10},
    {"Book", 12.0, 2},
    {"Bag", 25.0, 1}
};
items[1].qty = 3;

Cheat Sheet

Keep these facts handy while you work with Arrays of Structures in C.

TopicDetail
Elementarr[i] is a whole struct
Fieldarr[i].field
Countsizeof arr / sizeof arr[0]
Sortqsort with a comparator

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Pointers to Structures.

Example 1 — Order total from an array of items

Loop over records, print each line total, and sum them.

C
#include <stdio.h>

struct Item {
    const char *name;
    double price;
    int qty;
};

int main(void) {
    struct Item items[] = {
        {"Pen", 1.5, 10},
        {"Book", 12.0, 2},
        {"Bag", 25.0, 1}
    };
    int n = sizeof items / sizeof items[0];
    double total = 0;

    for (int i = 0; i < n; i++) {
        double line = items[i].price * items[i].qty;
        printf("%-5s x%d = %.2f\n", items[i].name, items[i].qty, line);
        total += line;
    }
    printf("Total: %.2f\n", total);
    return 0;
}

How It Works

  1. Each element is a complete struct Item.
  2. %-5s left-aligns names in five columns.
  3. 15 + 24 + 25 = 64.

Edge Cases & Pitfalls

Check these before relying on Arrays of Structures in real programs.

count

Hard-coded counts

Compute the count from sizeof so adding a record cannot desync the loop.

copy

Copying large structs in loops

Use const struct Item *it = &items[i]; to avoid repeated copies.

Key Takeaways

  • Table: one struct per row.
  • Loop: process records uniformly.
  • qsort: sort by any field.

One line: An array of structures holds a table of records, such as products or students.

Frequently Asked Questions

Use qsort with a comparison function that compares the chosen field of two elements passed as const void * pointers.
Divide the total size by the element size: sizeof items / sizeof items[0].
Continue with "C Pointers to Structures" next. Use the arrow operator to reach members through a struct pointer. The C Structures, Unions and Enums hub lists every topic in order.

Next: Pointers to Structures

Use the arrow operator to reach members through a struct pointer.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful