Programming in C

(Barry) #1

188 Chapter 9 Working with Structures


Or, you can initialize this variable to the same values with the following statement:
struct month aMonth = { 31, { 'J', 'a', 'n' } };
To go one step further, you can set up 12 month structures inside an array to represent
each month of the year:
struct month months[12];
Program 9.7 illustrates the monthsarray. Its purpose is simply to set up the initial values
inside the array and then display these values at the terminal.
It might be easier for you to conceptualize the notation that is used to reference par-
ticular elements of the monthsarray as defined in the program by examining Figure 9.3.

Program 9.7 Illustrating Structures and Arrays
// Program to illustrate structures and arrays

#include <stdio.h>

int main (void)
{
int i;

struct month
{
int numberOfDays;
char name[3];
};

const struct month months[12] =
{ { 31, {'J', 'a', 'n'} }, { 28, {'F', 'e', 'b'} },
{ 31, {'M', 'a', 'r'} }, { 30, {'A', 'p', 'r'} },
{ 31, {'M', 'a', 'y'} }, { 30, {'J', 'u', 'n'} },
{ 31, {'J', 'u', 'l'} }, { 31, {'A', 'u', 'g'} },
{ 30, {'S', 'e', 'p'} }, { 31, {'O', 'c', 't'} },
{ 30, {'N', 'o', 'v'} }, { 31, {'D', 'e', 'c'} } };

printf ("Month Number of Days\n");
printf ("----- --------------\n");

for ( i = 0; i < 12; ++i )
printf (" %c%c%c %i\n",
months[i].name[0], months[i].name[1],
months[i].name[2], months[i].numberOfDays);

return 0;
}
Free download pdf