Struc(Structures)tures

A structure is capable of containing any other type of C language variable. That includes other structures.

The structure-within-a-structure thing is kind of odd. It’s not really necessary unless you have a single structure that’s used twice, for instance, to reference the same type of data.

To better explain how nested structures work, consider the following code:

#include <stdio.h>
#include <string.h>

int main()
{
    struct date {
        int year;
        int month;
        int day;
    };
    struct called {
        char first[32];
        char last[32];
    };
    struct human {
        struct date birthday;
        struct called name;
    };
    struct human president;     /* declare structure variable */

/* fill structure data */
    strcpy(president.name.first,"George");
    strcpy(president.name.last,"Washington");
    president.birthday.year = 1732;
    president.birthday.month = 2;
    president.birthday.day = 22;

/* display results */
    printf("President %s %s was born on %d/%02d/%d.\n",
            president.name.first,
            president.name.last,
            president.birthday.month,
            president.birthday.day,
            president.birthday.year);

    return(0);
}

This code uses two structures, date and called, and creates a third structure human. The human structure contains the nested date and called structures by using the variables birthday and name, respectively. This method should be familiar to you from my books.

Here’s sample output:

President George Washington was born on 2/22/1732.

The problem here is that no pressing need demands the nested structures. They don’t solve any problems because the entire structure could be rewritten as:

struct human {
    int birthyear;
    int birthmonth;
    int birthday;
    char firstname[32];
    char lastname[32];
};

Other, obvious changes would be required in the code to reflect this new structure, but my point is that nothing is gained by using nested structures. The following code, however, demonstrates how the nested structures could be used:

#include <stdio.h>
#include <string.h>

int main()
{
    struct date {
        int year;
        int month;
        int day;
    };
    struct called {
        char first[32];
        char last[32];
    };
    struct human {
        struct called name;
        struct date birthday;
        struct date married;
        struct called spouse;
    };
    struct human president;     /* declare structure variable */

/* fill structure data */
    strcpy(president.name.first,"George");
    strcpy(president.name.last,"Washington");
    president.birthday.year = 1732;
    president.birthday.month = 2;
    president.birthday.day = 22;
    strcpy(president.spouse.first,"Martha");
    strcpy(president.spouse.last,"Curtis");
    president.married.year = 1759;
    president.married.month = 1;
    president.married.day = 6;

/* display results */
    printf("President %s %s was born on %d/%02d/%d.\n",
            president.name.first,
            president.name.last,
            president.birthday.month,
            president.birthday.day,
            president.birthday.year);
    printf("He married %s %s on %d/%02d/%d.\n",
            president.spouse.first,
            president.spouse.last,
            president.married.month,
            president.married.day,
            president.married.year);

    return(0);
}

In the code above, the two structures date and called are used twice within the human structure. That’s really how nested structures are useful, when they represent the same type of data but for two different purposes. In this case, both birthday and marriage day.

Here’s sample output:

President George Washington was born on 2/22/1732.
He married Martha Curtis on 1/06/1759.

Coming up next Lesson: pointers to structures. Yum!