I currently have the following code in my program:
struct mystruct
{
int x;
struct y *list[10];
}
mystruct *h1=(mystruct *)malloc(sizeof(mystruct));
I want to declare the array list dynamically (using malloc()) when I declare the structure. Could anyone tell me how to go about this?
You need to do it explicitly. I normally do this with a wrapper function like this.
Edit: Now there’s a complete working example at the bottom.
That way, you just call this in your code:
You’ll also want to write a corresponding
mystruct_free()function.Here’s a working example (for me):