I guess you would like to expand the capacity
of the array
in ArrayList
actually.
Then the function should be like:
#include <errno.h>
ArrayList *expandArrayList(ArrayList *list, int new_capacity){
if (list->capacity >= new_capacity) {
return list; // If a smaller capacity is passed in, do nothing.
}
char **tmp = realloc(list->array, new_capacity * sizeof(char*));
if (tmp) {
list->capacity = new_capacity;
list->array = tmp; // On success, set 'array' field of 'list' to tmp.
} else {
if (errno == ENOMEM) {
fprintf(stderr, "Error: not enough memory.\n"); // On failure, report the error and exit.
exit(1);
}
}
return list;
}
I have not tested the code, but I hope this would help you.