In the previous two puzzles, Triangle
structs
directly contained other structs.
Another possibility is to have the Triangle
struct
contain pointers to the other struct types.
Here is the picture:
Here are some typedef
definitions for this:
typedef struct { int x, y; } Point; typedef struct { int red, green, blue; } Color; typedef struct { Point *p0, *p1, *p2; Color *color; } TriangleMod;
Here is code that constructs such a TriangleMod
struct:
int main() { TriangleMod *trimod; trimod = (TriangleMod *)malloc( sizeof( TriangleMod ) ); trimod->p0 = (Point *)malloc( sizeof( Point ) ); trimod->p1 = (Point *)malloc( sizeof( Point ) ); trimod->p2 = (Point *)malloc( sizeof( Point ) ); trimod->color = (Point *)malloc( sizeof( Color ) ); ... }
No values have been initialized in the above. This is done by statements like:
trimod->p0->x = 45; triptr->p0->y = 97;