fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5. int docid;
  6. int freq;
  7. } pairs_t;
  8.  
  9. typedef struct {
  10. char *word;
  11. int numlines;
  12. pairs_t *pairs;
  13. int index;
  14. int npairs;
  15. } data_t;
  16.  
  17. typedef struct {
  18. data_t *data;
  19. int numwords;
  20. } index_t;
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. int initialsize = 1;
  25. int count = 0;
  26.  
  27. index_t *index;
  28. index = malloc(sizeof(index_t));
  29.  
  30. int required_size = 1;
  31.  
  32. index->data = malloc(required_size * sizeof(data_t));
  33.  
  34. //for first element of array
  35. index->data[0].pairs = malloc(required_size * sizeof(pairs_t));
  36. //you can even realloc
  37. index->data[0].pairs = realloc(index->data[0].pairs, 1 * sizeof(pairs_t));
  38.  
  39. //now you can access the members of pairs this way
  40.  
  41. index->data[0].pairs[0].docid = 777;
  42. index->data[0].pairs[0].freq = 777;
  43.  
  44. printf("docid : %d\nfreq : %d", index->data[0].pairs[0].docid, index->data[0].pairs[0].freq);
  45.  
  46. free(index->data[0].pairs);
  47. free(index->data);
  48. free(index);
  49.  
  50. count++;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
docid : 777
freq  : 777