Working with Structures in C: Structures Containing Arrays

As the heading of this section implies, it is possible to define structures that contain arrays as members.  One of the most common applications of this type is setting up an array of characters inside a structure. For example, suppose you want to define a struc- ture called month that contains as its members the number of days in the month as well as a three-character abbreviation for the month name. The following definition does the job:

struct month

{

int   numberOfDays;

char  name[3];

};

This sets up a month structure that contains an integer member called numberOfDays and a character member called name. The member name is actually an array of three charac- ters.You can now define a variable to be of type struct month in the normal fashion:

struct month aMonth;

You can set the proper fields inside aMonth for January with the following sequence of statements:

aMonth.numberOfDays = 31;

aMonth.name[0] = ‘J’;

aMonth.name[1] = ‘a’;

aMonth.name[2] = ‘n’;

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 months array. 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 months array 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;

}

Program 9.7   Output

As you can see in Figure 9.3, the notation

months[0]

refers to the entire month structure contained in the first location of the months array. The type of this expression is struct month. Therefore, when passing months[0] to a function as an argument, the corresponding formal parameter inside the function must be declared to be of type struct month.

Going one step further, the expression

months[0].numberOfDays

refers to the numberOfDays member of the month structure contained in months[0]. The type of this expression is int. The expression

months[0].name

references the three-character array called name inside the month structure of months[0]. If passing this expression  as an argument to a function, the corresponding formal param- eter is declared to be an array of type char.

Finally, the expression

months[0].name[0]

references the first character of the name array contained in months[0] (the character ‘J’).

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 *