Programming in C

(Barry) #1
Structures Containing Structures 185

Figure 9.2 The array testTimesin memory.

Structures Containing Structures


C provides you with an enormous amount of flexibility in defining structures. For
instance, you can define a structure that itself contains other structures as one or more of
its members, or you can define structures that contain arrays.
You have seen how it is possible to logically group the month, day, and year into a
structure called dateand how to group the hour, minutes, and seconds into a structure
called time. In some applications, you might have the need to logically group both a
date and a time together. For example, you might need to set up a list of events that are
to occur at a particular date and time.
What the preceding discussion implies is that you want to have a convenient means
for associating boththe date and the time together.You can do this in C by defining a
new structure, called, for example,dateAndTime, which contains as its members two ele-
ments: the date and the time.


.hour
.minutes
.seconds

11

59
12

0

23

.hour
.minutes
.seconds
.hour
.minutes
.seconds
.hour
.minutes
.seconds
.hour
.minutes
.seconds

testTimes[0]

testTimes[4]

testTimes[1]

testTimes[2]

testTimes[3]

59

0

1
29
59

59
59

12
27

19
Free download pdf