Page 103 - Programming Microcontrollers in C
P. 103

88     Chapter 2  Advanced C Topics

                          a floating-point number. The structure declaration must be followed
                          by a semicolon. This combination of variables is created every time
                          a structure of the type person is declared. The name person follow­
                          ing the struct declaration is called the structure tag or merely the
                          tag. Tags are optional. If a tag is used, it may be used to declare other
                          structures of the same type by
                   struct person ap,bp,cp;

                          Here ap,bp, and cp are structures of the type person. A single
                          instance of a structure can be declared by

                   struct {....} a;
                          In this case a is a structure with the elements defined between the
                          braces. The elements that make up a structure are called its mem­
                          bers. Members of a structure can be accessed by appending a period
                          followed by the member name to the structure name. For example,
                          the name of the person represented by ap is accessed by

                   ap.name
                          and that person’s salary is

                   ap.salary
                          A pointer to a structure can be used. The pointer pperson is created by

                   struct person *pperson;
                          If a pointer to a structure is used, members of the structure can be
                          accessed by the use of a special operator ->. This operator is created
                          by use of the minus sign (-) followed by the right angle bracket
                          character (>). The height of a person identified by the pointer
                          pperson is accessed by
                   pperson->height

                          Arrays of structures are used, and when dealing with pointers to ar­
                          rays of structures, to increment the pointer will move the pointer to
                          the next structure. If a program has

                   struct person people[20], *pp;
                          and pp is made to point at people[0] by

                   pp=people
   98   99   100   101   102   103   104   105   106   107   108