This has been such a pain. I'm trying to dynamically allocate an array and used realloc, calloc, and malloc but neither of the three has lead me to anything. It seems like I have successfully expanded it, but I have not been able to copy it correctly. Everything within the expand function is okay but after I call the function it becomes useless.
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;
}