A struct can contain any data type,
including arrays or other structs.
Define a struct type, using typedef
,
for a triangle in 2D computer graphics.
A triagle consists of three Points
and a Color.
Use three separate members for each of the Points of the triangle. Another possibility is to use an array of three Points, but this is perhaps more bother than it is worth.
/* declare the Point type */ /* declare the Color type */ /* declare the Triangle type */ /* function to print a Point */ void printPoint( Point *p ) {...} /* function to print a Color */ void printColor( Color *c ) {...} /* function to print a Triangle */ void printTriangle( Triangle *t ) {...} int main() { /* declare and initialize three Points and a Color */ Point p0={-32,77}, p1={345, 490}, p2={140, 389}; Color c={230, 120, 54}; /* declare and initialize a Triangle */ Triangle tri = {p0, p1, p2, c}; /* print the Triangle */ printTriangle( &tri); return 0; }