【发布时间】:2012-02-01 21:08:19
【问题描述】:
这是我必须在结构内动态声明数组的代码。我正在动态分配具有称为“容量”的大小的数组列表。在我的程序的稍后时刻,我想增加我的数组的大小并重新分配它。我该怎么办?
struct mystruct {
int x;
struct y **list;
};
用于声明结构中存在的数组的包装函数
struct mystruct *mystruct_init()
{
struct mystruct *mystruct = calloc(1, sizeof(*mystruct));
// loop through and allocate memory for each element in list
mystruct->list = calloc(1, sizeof(struct y *) * mystruct->list_length);
for (int i = 0; i < capacity; i++)
mystruct->list[i] = calloc(1, sizeof(struct y));
return mystruct;
}
调用包装函数
struct mystruct *h1 = mystruct_init();
我的问题是,如何使用 realloc 函数来增加列表的大小(容量值的两倍)?如果有人可以帮助我,那就太好了。
【问题讨论】:
-
@SethCarnegie 你的编辑杀死了一个需要的括号。
-
@Till 哎呀,固定,缩进你知道
标签: c