I have a bunch of structures declared like this:
typedef struct {
int docid;
int freq;
} pairs_t;
typedef struct {
char *word;
int numlines;
pairs_t *pairs;
int index;
int npairs;
} data_t;
typedef struct {
data_t *data;
int numwords;
} index_t;
Where I want to create an array of structures within index_t, where index_t will hold information about each element from data_t.
I am trying to
- malloc space for the array
data_t *datawithinindex_tto hold an array of structures. - realloc more space when required for the array of structures.
- malloc enough space for each element within the array of structures
I have just been playing around with this, and this is what I came up with:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[]) {
int initialsize = 1;
int count = 0;
index_t *index;
index->data = (data_t*)malloc(initialsize * sizeof(data_t));
index->data = realloc(index->data, 2 * initialsize);
index->data[count] = malloc(sizeof(data_t));
count++;
return 0;
}
I am just wondering why my mallocing of index->data[count] is causing an error. This is no means a proper program, I was just wondering why this isn't working. I am just trying to see If I can get all three of these steps to work before I attempt a bigger program.
The error is:
error: incompatible types when assigning to type "data_t" from type "void *"
Any sort of help would be appreciated.