Previous: 12.5 Common Errors
Up: 12 Structures and Unions
Next: 12.7 Exercises
Previous Page: 12.5 Common Errors
Next Page: 12.7 Exercises
In this chapter, we have described the last remaining data types provided by the C language: structures and unions. A structure allows the grouping of various pieces of related information of different types into one variable. It is declared by defining a template specifying the type of each data item stored in the structure and giving each member or field a name:
We have illustrated the use of structures with various programming examples.
Finally, we have described the union data type, which is defined similar to structures; however, has the semantics of only one of the member types being resident in such a variable at one time. That is, a union allows several different types of information to be stored in the same physical space at different times. For a union variable, storage is allocated only for the largest of the data types which may reside in the variable.
Structures are a valuable tool for developing complex programs and data structures in an efficient and top down manner.