20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Structures Conta<strong>in</strong><strong>in</strong>g Arrays<br />

187<br />

Naturally, it is possible to set up an array of dateAndTime structures, as is done with the<br />

follow<strong>in</strong>g declaration:<br />

struct dateAndTime events[100];<br />

The array events is declared to conta<strong>in</strong> 100 elements of type struct dateAndTime.The<br />

fourth dateAndTime conta<strong>in</strong>ed with<strong>in</strong> the array is referenced <strong>in</strong> the usual way as<br />

events[3],and the ith date <strong>in</strong> the array can be sent to your dateUpdate function as follows:<br />

events[i].sdate = dateUpdate (events[i].sdate);<br />

To set the first time <strong>in</strong> the array to noon, the series of statements<br />

events[0].stime.hour = 12;<br />

events[0].stime.m<strong>in</strong>utes = 0;<br />

events[0].stime.seconds = 0;<br />

can be used.<br />

Structures Conta<strong>in</strong><strong>in</strong>g Arrays<br />

As the head<strong>in</strong>g of this section implies, it is possible to def<strong>in</strong>e structures that conta<strong>in</strong><br />

arrays as members. One of the most common applications of this type is sett<strong>in</strong>g up an<br />

array of characters <strong>in</strong>side a structure. For example, suppose you want to def<strong>in</strong>e a structure<br />

called month that conta<strong>in</strong>s as its members the number of days <strong>in</strong> the month as well<br />

as a three-character abbreviation for the month name.The follow<strong>in</strong>g def<strong>in</strong>ition does<br />

the job:<br />

struct month<br />

{<br />

<strong>in</strong>t numberOfDays;<br />

char name[3];<br />

};<br />

This sets up a month structure that conta<strong>in</strong>s an <strong>in</strong>teger member called numberOfDays and<br />

a character member called name.The member name is actually an array of three characters.You<br />

can now def<strong>in</strong>e a variable to be of type struct month <strong>in</strong> the normal fashion:<br />

struct month aMonth;<br />

You can set the proper fields <strong>in</strong>side aMonth for January with the follow<strong>in</strong>g sequence of<br />

statements:<br />

aMonth.numberOfDays = 31;<br />

aMonth.name[0] = 'J';<br />

aMonth.name[1] = 'a';<br />

aMonth.name[2] = 'n';

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!