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.

Initializ<strong>in</strong>g Structures<br />

181<br />

sets the time1 variable to the same <strong>in</strong>itial values as shown <strong>in</strong> the previous example.The<br />

statement<br />

struct date today = { .year = 2004 };<br />

sets just the year member of the date structure variable today to 2004.<br />

Compound Literals<br />

You can assign one or more values to a structure <strong>in</strong> a s<strong>in</strong>gle statement us<strong>in</strong>g what is<br />

know as compound literals.For example, assum<strong>in</strong>g that today has been previously declared<br />

as a struct date variable, the assignment of the members of today as shown <strong>in</strong><br />

Program 9.1 can also be done <strong>in</strong> a s<strong>in</strong>gle statement as follows:<br />

today = (struct date) { 9, 25, 2004 };<br />

Note that this statement can appear anywhere <strong>in</strong> the program; it is not a declaration<br />

statement.The type cast operator is used to tell the compiler the type of the expression,<br />

which <strong>in</strong> this case is struct date, and is followed by the list of values that are to be<br />

assigned to the members of the structure, <strong>in</strong> order.These values are listed <strong>in</strong> the same<br />

way as if you were <strong>in</strong>itializ<strong>in</strong>g a structure variable.<br />

You can also specify values us<strong>in</strong>g the .member notation like this:<br />

today = (struct date) { .month = 9, .day = 25, .year = 2004 };<br />

The advantage of us<strong>in</strong>g this approach is that the arguments can appear <strong>in</strong> any order.<br />

Without explicitly specify<strong>in</strong>g the member names, they must be supplied <strong>in</strong> the order <strong>in</strong><br />

which they are def<strong>in</strong>ed <strong>in</strong> the structure.<br />

The follow<strong>in</strong>g example shows the dateUpdate function from Program 9.4 rewritten<br />

to take advantage of compound literals:<br />

// Function to calculate tomorrow's date – us<strong>in</strong>g compound literals<br />

struct date dateUpdate (struct date today)<br />

{<br />

struct date tomorrow;<br />

<strong>in</strong>t numberOfDays (struct date d);<br />

if ( today.day != numberOfDays (today) )<br />

tomorrow = (struct date) { today.month, today.day + 1, today.year };<br />

else if ( today.month == 12 ) // end of year<br />

tomorrow = (struct date) { 1, 1, today.year + 1 };<br />

else<br />

// end of month<br />

tomorrow = (struct date) { today.month + 1, 1, today.year };<br />

}<br />

return tomorrow;

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

Saved successfully!

Ooh no, something went wrong!