Working with Structures in C: Arrays of Structures

You have seen how useful the structure is in enabling you to logically group related ele- ments together. With the time structure, for instance, it is only necessary to keep track of one variable, instead of three, for each time that is used by the program. So, to handle 10 different times in a program, you only have to keep track of 10 different variables, instead of 30.

An even better method for handling the 10 different times involves the combination of two powerful features of the C programming language: structures and arrays. C does not limit you to storing simple data types inside an array; it is perfectly valid to define an array of structures. For example,

struct time experiments[10];

defines an array called experiments, which consists of 10 elements. Each element inside the array is defined to be of type struct time. Similarly, the definition

struct date birthdays[15];

defines the array birthdays to contain 15 elements of type struct date. Referencing a particular structure element inside the array is quite natural. To set the second birthday inside the birthdays array to August 8, 1986, the sequence of statements

birthdays[1].month = 8;

birthdays[1].day   = 8;

birthdays[1].year  = 1986;

works just fine. To pass the entire time structure contained in experiments[4] to a function called checkTime, the array element is specified:

checkTime (experiments[4]);

As is to be expected, the checkTime function declaration must specify that an argument of type struct time is expected:

void checkTime (struct time t0)

{

.

.

.

}

Initialization of arrays containing structures is similar to initialization of multidimensional arrays. So the statement

struct time runTime [5] =

{ {12, 0, 0}, {12, 30, 0}, {13, 15, 0} };

sets the first three times in the array runTime to 12:00:00, 12:30:00, and 13:15:00. The inner pairs of braces are optional, meaning that the preceding statement can be equiva- lently expressed  as

struct time runTime[5] =

{ 12, 0, 0, 12, 30, 0, 13, 15, 0 };

The following statement

struct time runTime[5] =

{ [2] = {12, 0, 0} };

initializes just the third element of the array to the specified value, whereas the statement

static struct time runTime[5] = { [1].hour = 12, [1].minutes = 30 };

sets just the hours and minutes of the second element of the runTime array to 12 and 30, respectively.

Program 9.6 sets up an array of time structures called testTimes. The program then calls your timeUpdate function from Program 9.5. To conserve space, the timeUpdate function is not included in this program listing. However, a comment statement is insert- ed to indicate where in the program the function could be included.

In Program 9.6, an array called testTimes is defined to contain five different times. The elements in this array are assigned initial values that represent the times 11:59:59, 12:00:00, 1:29:59, 23:59:59, and 19:12:27, respectively. Figure 9.2 can help you to under- stand what the testTimes array actually looks like inside the computer’s memory. A par- ticular time structure stored in the testTimes array is accessed by using the appropriate index number 0–4. A particular member (hour, minutes, or seconds) is then accessed by appending a period followed by the member name.

For each element in the testTimes array, Program 9.6 displays the time as represent- ed by that element, calls the timeUpdate function from Program 9.5, and then displays the updated time.

 

Program 9.6   Illustrating Arrays of Structures

// Program to illustrate arrays of structures

#include <stdio.h>

struct time

{

int hour;

int minutes;

int seconds;

};

int main (void)

{

struct time timeUpdate (struct time now);

struct time testTimes[5] =

{ { 11, 59, 59 }, { 12, 0, 0 }, { 1, 29, 59 },

  { 23, 59, 59 }, { 19, 12, 27 }};

int i;

for ( i = 0; i < 5; ++i ) {

printf (“Time is %.2i:%.2i:%.2i”, testTimes[i].hour,

testTimes[i].minutes, testTimes[i].seconds);

testTimes[i] = timeUpdate (testTimes[i]);

printf (” …one second later it’s %.2i:%.2i:%.2i\n”,

testTimes[i].hour, testTimes[i].minutes, testTimes[i].seconds);

}

return 0;

}

// ***** Include the timeUpdate function here *****

Program 9.6   Output

Time is 11:59:59 …one second later it’s 12:00:00

Time is 12:00:00 …one second later it’s 12:00:01

Time is 01:29:59 …one second later it’s 01:30:00

Time is 23:59:59 …one second later it’s 00:00:00

Time is 19:12:27 …one second later it’s 19:12:28

The concept of an array of structures is a very powerful and important one in C. Make certain you understand it fully before you move on.

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 *