假设我有一个名为 Thing 的结构。如果我想要一个“事物”数组,但它没有固定大小(动态),我该如何为它分配空间?我是否最初为数组本身分配空间,然后每次向其中添加元素时都必须重新分配空间?例如:
struct Thing{
char *stuff;
char **morestuff;
int evenmorestuff;
};
Thing *thingarray;
thingarray = malloc(sizeof(Thing));
....
//And then allocating space for elements, which will get called an unknown amount of times
Thing j;
thingarray[count] = j;
如何设置 malloc 和 realloc 以便能够将尽可能多的 Thing 类型的元素添加到“Thing”数组中?