Arrays of Structs

What Is an Array of Structures in C?

An array of structures in C stores several structs of one type side by side in memory. Here is how to declare, fill, and loop over one, with examples.

On this page
  1. What an array of structures is
  2. Defining the struct
  3. Declaring and filling the array
  4. Looping over the array
  5. How the elements are stored
  6. Passing the array to a function
  7. The C++ version
  8. Common mistakes
  9. Summary

What an array of structures is

An array of structures in C is an array in which each element is a struct. Each element holds one full record, with the same members as every other element. The records sit next to each other in memory, in order.

Defining the struct

The struct defines the layout of one record. In C, the type is named struct Item, so the struct keyword is needed each time you use the type:

struct Item {
    char name[32];
    int quantity;
    double price;
};

Declaring and filling the array

Use the struct as the element type, and list the records in braces:

struct Item items[3] = {
    {"Pen", 10, 1.25},
    {"Notebook", 4, 3.50},
    {"Stapler", 1, 12.00}
};

Each set of inner braces fills one struct, with its values in the order the members were declared.

Looping over the array

While items is still a real array, sizeof items / sizeof items[0] gives the number of elements. A for loop can then print each record:

#include <stdio.h>

struct Item {
    char name[32];
    int quantity;
    double price;
};

int main(void) {
    struct Item items[3] = {
        {"Pen", 10, 1.25},
        {"Notebook", 4, 3.50},
        {"Stapler", 1, 12.00}
    };
    size_t count = sizeof items / sizeof items[0];

    for (size_t i = 0; i < count; i++) {
        printf("%s: %d at %.2f\n", items[i].name, items[i].quantity, items[i].price);
    }
    return 0;
}

The program prints:

Pen: 10 at 1.25
Notebook: 4 at 3.50
Stapler: 1 at 12.00

How the elements are stored

Each element is a whole struct, so items[1] starts sizeof(struct Item) bytes after items[0]. The compiler may add padding between the members of a struct to meet alignment rules. Because of that, sizeof(struct Item) can be larger than the sizes of the members added together.

Passing the array to a function

When you pass items to a function, the function receives a pointer to the first element, not a copy of the array. Pass the element count along with it, as covered in how to pass an array to a function in c:

void print_items(const struct Item *list, size_t count) {
    for (size_t i = 0; i < count; i++) {
        printf("%s\n", list[i].name);
    }
}

print_items(items, count);

The C++ version

C++ lets you drop the struct keyword, so the type is simply Item. In C++ you can also use std::string for the name in place of a fixed char array:

struct Item {
    std::string name;
    int quantity;
    double price;
};

Item items[3] = {
    {"Pen", 10, 1.25},
    {"Notebook", 4, 3.50},
    {"Stapler", 1, 12.00}
};

Common mistakes

  • Leaving out the struct keyword in C. Writing Item items[3]; in C fails unless you have added a typedef for the type.
  • Going past the last element. items[3] is outside an array of three elements. Reading or writing it is undefined behavior.
  • Assigning a name with =. You cannot write items[0].name = "Pen"; in C, because a char array cannot be assigned with =. Use strcpy from <string.h> instead, and make sure the array has room for the text and the terminating null character.

Summary

An array of structures in C is a row of records that all share one struct type. Declare the struct once, use it as the element type, and loop with an index to read each record.

The complete guide to arrays of structs

Arrays of Structs in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Is C++ Harder Than Python?

For most beginners, C++ is harder than Python because it needs a compiler, explicit types and careful memory handling. See why, with a side-by-side example.

C++ vs Python 4 min read