fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LINELEN 100
  6. #define INITSIZE 10
  7.  
  8. typedef struct {
  9. int *array;
  10. int key;
  11. size_t used;
  12. size_t currsize;
  13. } line_t;
  14.  
  15. typedef struct {
  16. line_t *lines;
  17. int numlines;
  18. } array_t;
  19.  
  20. array_t *initialize_array(void);
  21. void generate_line(array_t *A, int index);
  22. void insert_into_array(array_t *A, int index, char *number);
  23. void printfree_array(array_t *A);
  24. void check_ptr(void *ptr, const char *msg);
  25.  
  26. int
  27. main(int argc, char *argv[]) {
  28. char line[LINELEN];
  29. char *key;
  30. array_t *A;
  31. size_t arrsize = INITSIZE, count = 0;
  32.  
  33. A = initialize_array();
  34.  
  35. A->lines = malloc(arrsize * sizeof(line_t));
  36. check_ptr(A->lines, "Allocation");
  37.  
  38. printf("Enter data into console:\n");
  39. while (fgets(line, LINELEN, stdin) != NULL && strlen(line) != 1) {
  40. if (arrsize == count) {
  41. arrsize *= 2;
  42. A->lines = realloc(A->lines, arrsize * sizeof(line_t));
  43. check_ptr(A->lines, "Reallocation");
  44. }
  45. generate_line(A, count);
  46. key = strtok(line, " :[],");
  47. A->lines[count].key = atoi(key);
  48. while ((key = strtok(NULL, " :[],\n")) != NULL) {
  49. insert_into_array(A, count, key);
  50. }
  51. count++;
  52. A->numlines++;
  53. }
  54.  
  55. printfree_array(A);
  56.  
  57. return 0;
  58. }
  59.  
  60. void
  61. printfree_array(array_t *A) {
  62. int i, j;
  63.  
  64. printf("Your array of structures:\n");
  65. for (i = 0; i < A->numlines; i++) {
  66. printf("key = %d, ", A->lines[i].key);
  67.  
  68. printf("array = [");
  69. for (j = 0; j < A->lines[i].used; j++) {
  70. printf("%d", A->lines[i].array[j]);
  71. if (j != A->lines[i].used-1) {
  72. printf(", ");
  73. }
  74. }
  75. free(A->lines[i].array);
  76. printf("]\n");
  77. }
  78. free(A->lines);
  79. free(A);
  80. }
  81.  
  82. void
  83. insert_into_array(array_t *A, int index, char *number) {
  84. if (A->lines[index].currsize == A->lines[index].used) {
  85. A->lines[index].currsize *= 2;
  86. A->lines[index].array = realloc(A->lines[index].array,
  87. A->lines[index].currsize * sizeof(int));
  88. check_ptr(A->lines[index].array, "Reallocation");
  89. }
  90. A->lines[index].array[A->lines[index].used++] = atoi(number);
  91.  
  92. }
  93.  
  94. void
  95. generate_line(array_t *A, int index) {
  96. A->lines[index].currsize = INITSIZE;
  97. A->lines[index].used = 0;
  98. A->lines[index].key = 0;
  99. A->lines[index].array = malloc(A->lines[index].currsize * sizeof(int));
  100. check_ptr(A->lines[index].array, "Allocation");
  101. }
  102.  
  103. array_t
  104. *initialize_array(void) {
  105. array_t *A = malloc(sizeof(*A));
  106. check_ptr(A, "Allocation");
  107. A->lines = NULL;
  108. A->numlines = 0;
  109. return A;
  110. }
  111.  
  112. void
  113. check_ptr(void *ptr, const char *msg) {
  114. if (!ptr) {
  115. printf("Unexpected null pointer: %s\n", msg);
  116. exit(EXIT_FAILURE);
  117. }
  118. }
Success #stdin #stdout 0s 2304KB
stdin
0 : [ 83, 42, 7 ]
21:[3, 6, 8, 12, 9, 3, 6, 8, 12]
63 : [ 8, 12, 9, 3, 6, 8 ]
0 : [ 20, 31, 70 ]
stdout
Enter data into console:
Your array of structures:
key = 0, array = [83, 42, 7]
key = 21, array = [3, 6, 8, 12, 9, 3, 6, 8, 12]
key = 63, array = [8, 12, 9, 3, 6, 8]
key = 0, array = [20, 31, 70]