CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Defining structures

Instead of storing and identifying related data as independent variables, we prefer to "collect" it all into a single structure.

C provides a mechanism to bring related data together, structures, using the struct keyword.

We can now define and gather together our related data with:


//  DEFINE AND INITIALIZE ONE VARIABLE THAT IS A STRUCTURE
struct {
    char    *name;   // a pointer to a sequence of characters
    int     red;     // in the range 0..255
    int     green;
    int     blue;
} rgb_colour = {
    "DodgerBlue",
     30,
    144,
    255
};

We now have a single variable (named rgb_colour) that is a structure, and at its point of definition we have initialised each of its 4 fields.

 


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