Page 110 - Programming Microcontrollers in C
P. 110
Structures 95
int radius,xc,yc;
center.x = (window.p1.x+window.p2.x)/2;
center.y = (window.p1.y+window.p2.y)/2;
xc = abs(window.p1.x - window.p2.x)/2;
yc = abs(window.p1.y - window.p2.y)/2;
radius=min(xc,yc);
cir=make_circle(center,radius);
draw_rectangle(window);
draw_circle(cir);
return 0;
}
The function make_circle() returns a type Circle and re
quires arguments Point and int. Circle is used to declare the
variable temp in make_circle(). Within the main program
Circle, Point, and Rect are used as types in the definition of
the several structure type variables used in the program. With the
typedef declarations, there is no basic change to the program, but
it is easier to read and follow.
The two functions draw_rectangle()and draw_circle()
are not standard C functions. These functions are programmed and
the listing of the final program is shown in Appendix B.
Self Referential Structures
A structure cannot contain itself as a member. Structure defini
tions are not recursive. However, a structure can contain a pointer to
a structure of the same type. This capability has proven quite useful
in dealing with complicated sort or listing problems. One sort prob
lem that can be easily treated is the binary tree sort. A tree sort receives
data, such as a word. Within the tree there is a root node that contains
a word. The node also contains a count of the number of times the
word has been seen and two pointers to additional nodes. These nodes
are called child or descendent nodes. Traditionally, the node to the
left contains a word that is less than the root word, and the node to
the right contains a word that is greater than the root node. As data
are read into the tree, the new word is compared with the root word.
If it is equal to the root word, the count in the node is incremented,