This is the code that I have to dynamically declaring an array inside a structure. I am dynamically allocating the array list with a size called ‘capacity’. At a later point in my program, I want to increase the size of my array and re-allocate it. How do I go about it?
struct mystruct {
int x;
struct y **list;
};
wrapper function to declare the array present inside the structure
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;
}
calling the wrapper function
struct mystruct *h1 = mystruct_init();
My question is, how do I use the realloc function to increase the size of list (double the value of capacity)? It would be really nice if someone could help me out.
assume you have
int oldsize: