Edit main()
of the following code so that
the struct is initialized correctly (pick the values you want)
and printed out.
At the end of the program, free dynamically allocated memory.
typedef struct
{
int x, y;
} Point;
void printPoint( Point *p )
{
printf("(%d, %d) ", p->x, p->y );
}
typedef struct
{
int red, green, blue;
} Color;
void printColor( Color *c )
{
printf("%3d red, %3d grn, %3d blu", c->red, c->green, c->blue );
}
typedef struct
{
Point *p0, *p1, *p2;
Color *color;
} TriangleMod;
void printTriangleMod( TriangleMod *t )
{
printf("Points: ");
printPoint( t->p0 ); printPoint( t->p1 ); printPoint( t->p2 );
printf("\tColor: ");
printColor( t->color );
printf("\n");
}
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 = (Color *)malloc( sizeof( Color ) );
/* Initialize trimod here */
/* Print out trimod here */
printTriangleMod( trimod );
/* Free memory here */
}
The triangle print function has been modified to use the pointers in
the TriangleMod
struct.
Dynamically allocated memory should be returned when it is no longer in use. Think carefully about how to free memory at the end of the program.