Puzzle T24


Style 2: Variable Points to a Triangle

Edit main() so that it contains the variable triptr which works correctly with the rest of the program. You will also need to allocate memory for the actual struct.

#include <stdio.h>
#include <stdlib.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: variable will point to a Triangle */
  ?????
  
  /* Allocate memory here */
  ????
  
  /* 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(????);
  
  /* free memory */
  ?????
  
} 

In production-quality code, it would be best to use an initialization function for Triangle structs and to have a function that changes their values.



Answer         Next Page         Previous Page Home