Programming in C

(Barry) #1

186 Chapter 9 Working with Structures


struct dateAndTime
{
struct date sdate;
struct time stime;
};
The first member of this structure is of type struct dateand is called sdate.The sec-
ond member of the dateAndTimestructure is of type struct timeand is called stime.
This definition of a dateAndTimestructure requires that a datestructure and a time
structure have been previously defined to the compiler.
Va r iables can now be defined to be of type struct dateAndTime, as in
struct dateAndTime event;
To r eference the datestructure of the variable event, the syntax is the same:
event.sdate
So, you could call your dateUpdatefunction with this date as the argument and assign
the result back to the same place by a statement such as
event.sdate = dateUpdate (event.sdate);
You can do the same type of thing with the timestructure contained within your
dateAndTimestructure:
event.stime = timeUpdate (event.stime);
To r eference a particular member insideone of these structures, a period followed by the
member name is tacked on the end:
event.sdate.month = 10;
This statement sets the monthof the datestructure contained within eventto October,
and the statement
++event.stime.seconds;
adds one to the secondscontained within the timestructure.
The eventvariable can be initialized in the expected manner:
struct dateAndTime event =
{ { 2, 1, 2004 }, { 3, 30, 0 } };
This sets the date in the variable eventto February 1, 2004, and sets the time to
3:30:00.
Of course, you can use members’ names in the initialization, as in
struct dateAndTime event =
{ { .month = 2, .day = 1, .year = 2004 },
{ .hour = 3, .minutes = 30, .seconds = 0 }
};
Free download pdf