G24 Answer


#include <stdio.h>
/* Practice with various ways to declare things */

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;
} Triangle;

void printTriangle( Triangle *t )
{
  printf("Points: ");
  printPoint( &t->p0 ); printPoint( &t->p1 ); printPoint( &t->p2 );
  printf("\nColor: ");
  printColor( &t->color );
  printf("\n");
}

int main ()
{
  /* Declare a variable here */
  Triangle *triptr;
  
  /* Allocate memory here */
  triptr = (Triangle *)malloc( sizeof(Triangle) );

  /* Leave the following unchanged */
  triptr->p0.x = 15;
  triptr->p0.y = 15;
  triptr->p1.x = 85;
  triptr->p1.y = 110;
  triptr->p2.x = 50;
  triptr->p2.y = 50;
  triptr->color.red   = 123;
  triptr->color.green = 50;
  triptr->color.blue = 150;

  /* Insert the parameter in the function call */
  printTriangle( triptr );
  
  /* free memory */
  free( triptr );
  
  system("pause");
}

Comments: