I'm having some trouble using an array of structs. I need to allocate sufficient memory then pass a pointer to this to a function that will create the array of structs, so that I can use the pointer later to search this array. It looks something like this (I've cut out much that is irrelevant):
This doesn't work at all, probably because I don't understand the syntax properly (I'm not a C or C++ programmer) or have perpetrated some other basic error(s). Would someone mind pointing the error(s) out?Code:typedef struct { char filename[128]; char group[32]; int type; } Experiment; void fill(Experiment * exptarr) { int count = 0; // parse a file to extract the data for each of maxexpts Experiments, // and attempt to copy the data to the array of structs { Experiment experiment; strcpy(experiment.filename,filename); strcpy(experiment.group,group); experiment.type = type; exptarr[count] = experiment; count++; } } int main(int argc, char * argv[]) { Experiment * exptarr; int maxexpts = 0; // code to count the maximum number of experiments goes here, // and updates maxexpts exptarr = malloc(sizeof(Experiment) * maxexpts); fill(&exptarr); // expect to find that exptarr is an pointer to an array of Experiment structs... }