/* commentedPoint.c */
#include <stdio.h>
#include <string.h>


typedef struct _Point {
  int x, y;
} Point;


typedef struct _CommentedPoint {
  Point pt;
  char *cp;
} CommentedPoint;




CommentedPoint *createCommentedPoint(int x, int y, char *commentp) {
  CommentedPoint *commp = malloc(sizeof(CommentedPoint));
  if (!commp) return NULL;

  commp->cp = malloc(strlen(commentp) * sizeof(char) + sizeof('\0'));
  if (!commp->cp) {
    free(commp);
    return NULL;
  }

  strcpy(commp->cp, commentp);
  commp->pt.x = x;
  commp->pt.y = y;
  return commp;
}



CommentedPoint *cloneCommentedPoint(CommentedPoint *cp) {
  return createCommentedPoint(cp->pt.x, cp->pt.y, cp->cp);
}



void deleteCommentedPoint(CommentedPoint *cp) {
  free(cp->cp);
  free(cp);
}



void printCommentedPoint(CommentedPoint *cp) {
  printf("x: %i, y: %i, cp: %s", cp->pt.x, cp->pt.y, cp->cp);
}



enum {LEN = 80};

int main() {
  CommentedPoint **cp = NULL;
  char line[LEN + 1];
  int i = 0;
  int j = 0;
  int x = 100;
  int y = 6000;
  char *chrp;

  while (printf("%i> ", i), chrp = fgets(line, LEN, stdin),
         !(chrp == NULL || *chrp == '\n')) {
    cp = (CommentedPoint **)realloc(cp, sizeof(CommentedPoint **) * ++i);
    if (!cp) {
      i--;
      puts("Out of memory (1)");
      break;
    }
    *(cp + i - 1) = createCommentedPoint(x++, --y, line);
    if (!*(cp + i - 1)) {
      i--;
      cp = (CommentedPoint **)realloc(cp, sizeof(CommentedPoint **) * i);
      puts("Out of memory (2)");
      break;
    }
  }

  while (j < i) {
    printCommentedPoint(*(cp + j));
    deleteCommentedPoint(*(cp + j));
    j++;
  }

  free(cp);
  cp = NULL;

  return 0;
}
