【发布时间】:2014-02-26 09:26:55
【问题描述】:
这太痛苦了。我正在尝试动态分配一个数组并使用 realloc、calloc 和 malloc,但是这三个都没有让我做任何事情。好像我已经成功扩展了它,但是我无法正确复制它。 expand 函数中的一切都很好,但是在我调用该函数之后它就变得没用了。
typedef struct ArrayList
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;
// Size of list (i.e., number of elements that have been added to the array)
int size;
// Length of the array (i.e., the array's current maximum capacity)
int capacity;
} ArrayList;
ArrayList *expandArrayList(ArrayList *list, int length){
struct ArrayList *temp=realloc(list, length);
if (length<list->capacity){
return NULL;
free(temp);
}
if (temp)
list=temp;
else{
free(temp);
return NULL;
}
list->capacity=length;
printf("-> Expanded ArrayList to size %d.\n", length);
return list;
}
【问题讨论】:
标签: c arrays pointers dynamic expand