Structs and Arrays in C: A Comprehensive Guide

Structs and Arrays in C: A Comprehensive Guide

How to Use Structs and Arrays to Store and Access Data

Play this article

Introduction

In my earlier article, I have written extensively on Structs and Typedef, So do well to check it.

In this article, I will be focusing more on how you can work with a struct of an array as well as an array of struct. You can also do well to check the article on arrays in case you want to revise what an array is.

We will be considering or handling two scenarios here, which are:

  • Arrays of Structs

  • Structs of Arrays

Working with Arrays of Structs

An array of struct is simply the declaration of a Struct as an array. That is to say that the Struct definition might or might not contain an array, depending on the elements and what the elements will be contained, if an element will take a string, we can either decide to use a pointer or simply declare it as an array with a size that is big enough to contain whatsoever is going to be stored in that element.

Let us take for instance we want to store the names, and ages of about 5 persons, in this case, we can take advantage of arrays of structs to carry this out. Here we just need to first define a struct with the name and age element, we will also be using a typedef in this case to give the struct data type a unique data type we can use.

Then in our main function, we will be declaring the struct as an array with a size of 5, to be able to accommodate the information of those 5 people.

 #include <stdio.h>

typedef struct
{
    char *name;
    int age;
} psn_t;

int main(void)
{
    int i;
    psn_t people[5] = {
                        { "Gideon", 75 },
                        { "Funom", 95 },
                        { "Simon", 64 },
                        { "Kurah", 86 },
                        { "Bature", 52 },
                    };

    for (i = 0; i < 5; i++)
    {
        printf("\nPerson %d\n", (i + 1));
        printf("Name: %s\n", people[i].name);
        printf("Age: %d\n", people[i].age);
    };
    return (0);
}

The code above will output all the names registered in the array, we entered the details of each person as defined by our struct, which is the name followed by the age, both separated by a comma. Each of them is inside curly braces just as how name and age are defined inside the struct enclosed by curly braces. Each of these braces is separated by a comma, just like how each element in an array are separated by a comma.

Working with Structs of Arrays

A struct of an array is simply a struct whose members are arrays. It means that one or more of the member(s) or element(s) of the array are arrays.

So let us look at this example, let's say we have a student who wrote 4 tests, and we want to collect both the student's name and also his scores in all 4 subjects.

How we go about this is to define our struct with name and score as members, and since we want to store 4 scores for this particular student, it has to be in an array, so we will have to give the score an array size of 4, as that will be enough to contain all the 4 scores. So let us implement this in our code.

#include <stdio.h>

typedef struct
{
    char *name;
    int score[4];
} stu_t;

int main(void)
{
    int i;
    stu_t student = {
        "Gideon",
        { 35, 28, 32, 40 }
    };

    printf("Name: %s\n", student.name);

    printf("\nScores\n\n");

    for (i = 0; i < 4; i++)
    {
        printf("Test %d: %d\n", (i + 1), student.score[i]);
    }
    return (0);
}

In the code above, to declare and also give the struct variable a value, we also follow the struct definition, we have two members, which are the name, and score, but the score is to hold 4 different values which is why we have to make it an array that can take in 4 different integers, so while feeding in our data, we entered the name, followed by the array of the scores, both separated by a comma. And these scores are enclosed inside curly braces just like how we put elements of an array in curly braces, with each element separated by a comma.

Conclusion

This is how you can apply an array to a struct and a struct to an array. We also have situations where struct work with functions, those functions, in particular, are known as function pointers, that is what we will be writing about next, there we will be explaining how to do that.

Thank you for reading. You can connect with me on Twitter and LinkedIn.