可能重复:
How can I create a dynamically sized array of structs?
我是C的新手并且在线学习确实很有帮助,但现在我真的被卡住了。我希望有一个结构数组ACout,大小为ACcount
typedef struct{
int outcount;
int *lingoout;
double **likegoout;
} ACout;
// int outcount是数组lingoout的大小,likegoout是一个2d数组,行数为outcount,列数为4
// int outcount将被计算,并且每个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;
}
感谢您的帮助!非常感激!!! ---海伦