8 - STRUCTURES, UNIONS
Published online by Cambridge University Press: 14 January 2010
Summary
This chapter explains the concept of a structure as a collection of variables, this collection including nested structures if desired.
A structure can be handled in much the same way as a variable; you can copy a structure, assign to a structure, take the address of a structure with &, access the members of a structure. You may declare arrays of structures. You can nominate a structure as a parameter of a function or write a function that returns a structure.
This chapter introduces structures by analogy with arrays. The operators for accessing members are defined and their use explained Concepts are illustrated by an example of a library list in which to search for a book if you know its title or can remember only part of its title.
Unions and bitfields are introduced (a union is a structure in which members share storage space).
Having described structures and unions it is possible to define, fully, the syntax terms type and declaration. The allowable syntax of declaration differs according to context, so a separate diagram is drawn for each context.
Finally the chapter explains the idea of stacks and gives an example of their use in converting simple algebraic expressions to reverse Polish notation.
INTRODUCING STRUCTURES
Much of information handling is about updating and sorting lists of names and addresses. With each name and address may come other information: an amount of money owing, a list of diseases survived, a code indicating the subject's purchasing power or likelihood of signing an order.
- Type
- Chapter
- Information
- Illustrating C , pp. 125 - 142Publisher: Cambridge University PressPrint publication year: 1994