Working with Structures in C: 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 date and 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 both the 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.

struct dateAndTime

{

struct date  sdate;

struct time  stime;

};

The first member of this structure is of type struct date and is called sdate. The sec- ond member of the dateAndTime structure is of type struct time and is called stime. This definition of a dateAndTime structure requires that a date structure and a time structure have been previously defined to the compiler.

Variables can now be defined to be of type struct dateAndTime, as in

struct dateAndTime event;

To reference the date structure of the variable event, the syntax is the same:

event.sdate

So, you could call your dateUpdate function 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 time structure contained within your

dateAndTime structure:

event.stime = timeUpdate (event.stime);

To reference a particular member inside one of these structures, a period followed by the member name is tacked on the end:

event.sdate.month = 10;

This statement sets the month of the date structure contained within event to October, and the statement

++event.stime.seconds;

adds one to the seconds contained within the time structure.

The event variable can be initialized in the expected manner:

struct dateAndTime event = { { 2, 1, 2004 }, { 3, 30, 0 } };

This sets the date in the variable event to 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 }

};

Naturally, it is possible to set up an array of dateAndTime structures,  as is done with the following declaration:

struct dateAndTime events[100];

The array events is declared to contain 100 elements of type struct dateAndTime. The fourth dateAndTime contained within the array is referenced in the usual way as events[3], and the ith date in the array can be sent to your dateUpdate function as fol- lows:

events[i].sdate = dateUpdate (events[i].sdate);

To set the first time in the array to noon, the series of statements

events[0].stime.hour = 12;

events[0].stime.minutes = 0;

events[0].stime.seconds = 0;

can be used.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *