可能重复:
如何创建动态大小的结构数组?
我是 C 新手,在线学习真的很有帮助,但现在我真的被困住了。我想要一个结构数组 ACout,大小为 ACcount
typedef struct{
int outcount;
int *lingoout;
double **likegoout;
} ACout;
// int outcount 是数组 lingoout 的大小,likegoout 是一个二维数组,其中行数为 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;
}
谢谢你的帮助!非常感激!!!---海伦