Page 108 - Programming Microcontrollers in C
P. 108

Structures     93

                              While it would not be difficult to execute these calculations without
                          the use of structures, it is obvious that the structure formulation of the
                          program makes a much simpler and easier to follow program. The vari­
                          ables and program elements are objects here rather than mere numbers.
                              C has a command called typedef. This command can rename
                          a specified type for the convenience of the programmer. It does not
                          create a new type, it merely renames an existing type. For example,
                   typedef int Miles;
                   typedef char Byte;
                   typedef int Word;
                   typedef long Dword;
                          are all valid typedef statements. After the above invocations, a
                          declaration

                   Miles m;
                   Byte a[20];
                   Word word;
                   Dword big;
                          would make m an int, a an array of 20 characters, word the type
                          int, and big a long. All that has happened is that these types are
                          a redefinition of the existing types. New types defined by typedefs
                          are usually written with an upper case first letter. This is a tradition,
                          not a requirement of the C language.
                              Structures used earlier could be modified by use of the typedef.
                          Consider

                   typedef struct
                   {
                       int x;
                       int y;
                   } Point;

                              This typedef redefines the earlier struct point as Point. The
                          judicious use of typedefs can make a program even easier to read
                          than the simple use of structs. The program that follows is the
                          same as that above where all of the structures are typedef new names.

                   /* Inscribe a circle in a rectangle */
   103   104   105   106   107   108   109   110   111   112   113