twitter
    Find out what I'm doing, Follow Me :)

A New Concept, The Typedef

Next we define a new type of data with a "typedef". This defines a complete new type that can be used in the same way that "int" or "char" can be used. Notice that the structure has no name, but at the end where there would normally be a variable name there is the name "BOATDEF". We now have a new type, "BOATDEF", that can be used to define a structure anyplace we would like to. Notice that this does not define any variables, only a new type definition.

Capitalizing the nameis a personal preference only and is not aCstandard. It makes the "typedef" look different from a variable name.We finally come to the big structure that defines our data using the building blocks already defined above. The structure is composed of 5 parts, two simple variables named "vehicle" and "weight", followed by the union, and finally the last two simple variables named "value" and "owner". Of course the union is what we need to look at carefully here, so focus on it for the moment. You will notice that it is composed of four parts, the first part being the variable "car" which is a structure that we defined previously. The second part is a variable named "boat" which is a structure of the type "BOATDEF" previously defined. The third part of the union is the variable "airplane" which is a structure defined in place in the union.

Finally we come to the last part of the union, the variable named "ship" which is another structure of the type "BOATDEF". I hope it is obvious to you that all four could have been defined in any of the three ways shown,
but the three different methods were used to show you that any could be used. In practice, the clearest definition would probably have occurred by using the "typedef" for each of the parts.