CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Defining an array of structures

Returning to our AFL project example, we can now define and gather together its related data with:


//  DEFINE THE LIMITS ON PROGRAM'S DATA-STRUCTURES
#define MAX_TEAMS               24
#define MAX_TEAMNAME_LEN        30
....

struct {
    char    teamname[MAX_TEAMNAME_LEN+1];        // +1 for null-byte

//  STATISTICS FOR THIS TEAM, INDEXED BY EACH TEAM'S 'TEAM NUMBER'
    int     played;
    int     won;
    int     lost;
    int     drawn;
    int     bfor;
    int     bagainst;
    int     points;
} team[MAX_TEAMS];    //  DEFINE A 1-DIMENSIONAL ARRAY NAMED team

We now have a single (1-dimensional) array, each element of which is a structure.
We often term this an array of structures.

Each element of the array has a number of fields, such as its teamname (a whole array of characters) and an integer number of points.

 


CITS2002 Systems Programming, Lecture 6, p12, 8th August 2023.