A structure in C can be seen as a extensionof the Array we have seen before.
In an Array, we have a group of variable of one particular type, each of them identified by an index. float arg[50] does mean nothing else than the 50 floating point numbers arg[0], arg[1], ..., arg[50].
The stucture now allows us to group even different types of variables together.
For example:
struct gun
{
char name[50];
int magazinesize;
float calibre;
};
struct gun arnies;
defines a new structure gun and makes arnies an instance of it.
NOTE: 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 gun
{
char name[50];
int magazinesize;
float calibre;
} arnies;
struct's also can be pre-initialised at declaration:
~struct gun arnies={"Uzi",30,7};
which gives arnie a. Uzi with 30 rounds of ammunition. of calibre 7mm.
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;
or, to disarm him (I like that better):