A struct
is a bundle of variables that
has a name.
The variables of a struct
are called its
members.
For example, the following struct
has
the name bulb
and contains two members,
watts
and lumens
.
Think of the struct
as representing a light bulb
that has particular values for watts and lumens.
struct { int watts; int lumens; } bulb;
The declaration allocates memory for the struct
and provides for a way to access its
members using dot notation:
bulb.watts
and bulb.lumens
.
The syntax of struct
declaration is this:
struct tag { member-list } variable-list;
The tag is the name of the type of struct
being declared.
The tag is optional if there is a variable-list.
The member-list is a list of items
that make up this struct.
The variable-list is a list of identifiers.
Each identifier is the name of a variable of this type of struct
.
The variable-list is optional if there is a tag.
Here is a declaration of three variables:
struct { int watts; int lumens; } bulb20, bulb60, bulb100;
Here is a declaration of a struct with a tag, but no variables.
struct BulbType { int watts; int lumens; };
Following the latter declaration, variables may be declared by using the struct's tag:
struct BulbType bulb20, bulb60, bulb100;