Possible Duplicate:
How can I create a dynamically sized array of structs?
I'm new to C and learning online really helps but now i'm really stuck. I want to have an array of structures ACout, with size ACcount
typedef struct{
int outcount;
int *lingoout;
double **likegoout;
} ACout;
// int outcount is the size of the array lingoout and likegoout is a 2d array with number of rows being outcount and number of columns 4
//int outcount will be calculated and will be different for each ACout.
int ACcount=0; //global, outside main
in main (){
//HOW TO INITIALIZE THE ARRAY OF STRUCTS???
//LIKE THIS?
ACout* ACout_array = (ACout *) calloc (1, sizeof (ACout));
if ( ACout_array == NULL) {
printf("Cannot allocate initial memory for data\n");
exit(1);
}
}
in some function {
ACcount++;
//int lingoout is computed here, as n;
//also array lingout, as lin[n]
//2d array likegoout, as likeout[n][4] is ready here
//how can I write a function to add one more structs to the array?
addarray(ACcount, AC_array);
//how can I put my calculated values to the structs?
AC_array[ACcount]->outcount=n;
AC_array[ACcount]->lingoout=lin;
AC_array[ACcount]->likegoout=likeout;
}
void addACarray (ACout **ACout_array, int ACcount) {
ACout *temp = (ACout*)realloc(*ACout_array, (ACcount * sizeof(ACout)));
if(temp == NULL) {
printf("Cannot allocate growarray memory!\n");
}
else
*ACout_array = temp;
}
Thanks for your help! Much appreciated!!! ---Helen
typedef struct Something { int outcount; int *lingoout; double **likegoout; } ACout;?