Page 105 - Programming Microcontrollers in C
P. 105

90     Chapter 2  Advanced C Topics

                       hold.y=y;
                       return hold;
                   }
                              Observe that the struct point is treated as a type with no diffi­
                          culty in this function. The return type is struct point, and within
                          the body of the function hold is also a type struct point. The x
                          argument passed to the function is placed in the x member of hold as
                          is the y argument placed in the y member. Then the struct hold is
                          returned to the calling function. All of these operations are legal.
                              Since structures create types, structures can have members that
                          are structures. For example, suppose that the struct rect for a
                          rectangle is defined as
                   struct rect
                   {
                       struct point p1;
                       struct point p2;
                   };

                              Let’s outline a program that will inscribe a circle within a rect­
                          angle. The circle is tangent to the sides that make up the narrowest
                          dimension of the rectangle.

                   /* Inscribe a circle in a rectangle */
                   /* first declare some useful structures */

                   struct point
                   {
                       int x;
                       int y;
                   };

                   struct circle
                   {
                       struct point center;
                       int radius;
                   };


                   struct rect
                   {
   100   101   102   103   104   105   106   107   108   109   110