Next: Defining New Data Types Up: Further Data Types Previous: Further Data Types

Structures

Structures in C are similar to records in Pascal. For example:

defines a new structure gun and makes arnies an instance of it.

NOTE: that gun is a tag for the structure that serves as shorthand for future declarations. We now only need to say struct gun and the body of the structure is implied as we do to make the arnies variable. The tag is optional.

Variables can also be declared between the } and ; of a struct declaration, i.e.:

struct's can be pre-initialised at declaration:

~struct gun arnies={"Uzi",30,7};
which gives arnie a. Uzi with 30 rounds of ammunition.

To access a member (or field) of a struct, C provides the . operator. For example, to give arnie more rounds of ammunition:

arnies.magazineSize=100;



Dave.Marshall@cm.cf.ac.uk
Fri May 20 13:40:49 BST 1994