13.07.2015 Views

C struct, typedef, enum & union - Tenouk C & C++

C struct, typedef, enum & union - Tenouk C & C++

C struct, typedef, enum & union - Tenouk C & C++

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

STRUCT, TYPEDEF, ENUM & UNION• We have declared the <strong>struct</strong>ure, inventory, at the top of the sourcefile.• This is called an external declaration and the scope is the entire fileafter the declaration.• Since all the functions in the file use this <strong>struct</strong>ure tag, the <strong>struct</strong>uremust be visible to each of them.• The main() calls readpart() to read data into a <strong>struct</strong>ure and returnit to be assigned to the variable, items.• Next, it calls printpart() passing items which merely prints thevalues of the <strong>struct</strong>ure fields, one at a time.• External declarations of <strong>struct</strong>ure templates and prototypes facilitateconsistent usage of tags and functions.• Sometimes, external <strong>struct</strong>ure tag declarations will be placed in aseparate header file, which is then made part of the source file by aninclude directive (#include).• From this example, we can see that using <strong>struct</strong>ures with functions isno different than using any scalar data type like int.www.tenouk.com, © 34/93

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

Saved successfully!

Ooh no, something went wrong!