< previous page page_784 next page >

Page 784
struct DateType
{
    int month;  // Assume 1..12
    int day;    // Assume 1..31
    int year;   // Assume 1900..2050
};
struct StatisticsType
{
    float    failRate;
    DateType lastServiced;
    int      downDays;
};
struct MachineRec
{
    int           idNumber;
    String50      description;
    StatisticsType history;
    DateType       purchaseDate;
    float          cost;
};

MachineRec inventory[NUM_MACHINES];
MachineRec machine;
int        counter;
The contents of a machine record are now much more obvious. Two of the components of the struct type MachineRec are themselves structs: purchaseDate is of struct type DateType, and history is of struct type StatisticsType. One of the components of struct type StatisticsType is a struct of type DateType.
How do we access a hierarchical structure such as this one? We build the accessing expressions (member selectors) for the members of the embedded structs from left to right, beginning with the struct variable name. Following are some expressions and the components they access.
ExpressionComponent Accessed
machine.purchaseDateDateType struct variable
machine.purchaseDate.monthmonth member of a DateType struct variable
machine.purchaseDate.yearyear member of a DateType struct variable
machine.history.lastServiced.yearyearmember of a DateType struct variable contained in a struct of type StatisticsType

Figure 14-4 is a pictorial representation of machine with values. Look carefully at how each component is accessed.

 
< previous page page_784 next page >