Defining New Data Types 
typedef can also be used with
structures. The following creates a new type agun which is of type struct gun and can be initialised as
usual: 
              typedef
struct gun
                            {
                            char
name[50];
                            int
magazinesize;
                            float
calibre;
                            }
agun;
               agun arnies={"Uzi",30,7};
Here gun
still acts as a tag to the struct and is optional. Indeed
since we have defined a new data type it is not really of much use, agun is the
new data type. arnies is a variable of type agun which is a structure. 
C also allows
arrays of structures: 
 
            typedef struct gun
                            {
                            char
name[50];
                            int
magazinesize;
                            float
calibre;
                            }
agun;
              agun
arniesguns[1000];
This gives
arniesguns a 1000 guns. This may be used in the following way: 
          arniesguns[50].calibre=100;
gives Arnie's
gun number 50 a calibre of 100mm, and: 
          itscalibre=arniesguns[0].calibre;
assigns the
calibre of Arnie's first gun to itscalibre. 
No comments:
Post a Comment