fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct data_list {
  6. struct data_list *next;
  7. char data[];
  8. };
  9.  
  10. int main(int argc, char **argv)
  11. {
  12. int res;
  13. char buf[1024];
  14. struct data_list data_list = { 0 }, *p;
  15. int l;
  16.  
  17. fputs("文字列を入力してください。ctrl-dで終了:\n", stderr);
  18. for (;;) {
  19. fputs("? ", stderr);
  20. if (fgets(buf, sizeof(buf) - 1, stdin) == NULL)
  21. break;
  22. l = strlen(buf);
  23. for (p = &data_list; p->next != NULL; p = p->next);
  24. p->next = malloc(sizeof(data_list) + l + 1);
  25. if (p->next == NULL) {
  26. perror("Mallocに失敗しました");
  27. res = 1;
  28. goto PG_END;
  29. }
  30. p->next->next = NULL;
  31. strcpy(p->next->data, buf);
  32. }
  33. putc('\n', stderr);
  34.  
  35. for (p = data_list.next; p != NULL; p = p->next)
  36. fputs(p->data, stdout);
  37.  
  38. res = 0;
  39. PG_END:;
  40. // free
  41. for (p = data_list.next; p; p = p->next)
  42. free(p);
  43. return res;
  44. }
Success #stdin #stdout 0.01s 1812KB
stdin
spring
samsun
nikibi
stdout
spring
samsun
nikibi