I am trying to implement something that requires a structure like this:
struct abc
{
int size;
struct abc *links[size];
}
Here, I want size to change at runtime, not just merely be different for each instance of abc, and instances of abc have varying number of links depending on the program. How do I create/manage/allocate memory for such a data structure? Is it even possible in C?
Easyest way would be to change your structure like this:
and calloc the array dynamically:
Then you can address the elements of the links array this way:
Be careful of not trying to access elements outside the boundaries of the calloc’ed array or you will get memory exceptions and unexpected behavior. Remember also to free(abc.links) for each allocated structure to prevent memory leaks…
Hope this helps