Puzzle T22


Variable Style 2: Variable Points to a struct

This program is nearly the same as the previous one, except now dynamic memory is used for the struct. Edit main() so that it contains the variable point which works correctly with the rest of the program. Use malloc() to get dynamic memory. Don't forget to free it at the end of the program.

#include <stdio.h>
#include <stdlib.h>

/* declare the Point type */
typedef struct
{
  int x, y;
} Point;

/* function to print a Point */
void printPoint( Point *p )
{
  printf("(%d, %d)\n", p->x, p->y );
}

int main()
{
  /* declare a pointer variable and allocate memory for a Point */
  Point ?????

  /* don't change these statements */
  point->x = -23;
  point->y = 45;
 
  /* print values */
  printPoint(   );

  /* free memory */
  free( ???? );

  return 0;
}


Answer         Next Page         Previous Page Home