The code below should compile in what you seem to want. There are however a lot of issues with what you're doing that may not be immediately obvious, but I'll go into that a little below.
typedef struct point {
int x;
} point;
typedef struct polygon {
// dynamically allocated array of point
point *point;
} polygon;
typedef struct form {
// dynamically allocated array of polygon
polygon *polygon;
} form;
form *vform;
vform = malloc(sizeof(form) * 5);
vform->polygon = malloc(sizeof(polygon) * 5);
vform->polygon->point = malloc(sizeof(point) * 5);
vform->polygon->point->x = 1;
First issue is that you're mixing up your variables with the members of your structures. Your form
structure has no element called "vpolygon". Just because your "vpolygon" variable is a polygon
structure doesn't mean you can suddenly refer to the polygon
member of a form
variable by calling it "vpolygon".
Secondly, you're casting the return value of malloc
. Don't do this. See do I cast the result of malloc
Thirdly, your form malloc
is allocationing enough memory for 5 form structures, but casting the result to a pointer to a pointer to form. The malloc should possibly be malloc(sizeof(form *) * 5)
but I'm guessing you really meant (vform *)malloc...
not (vform **)malloc...
.
Fourth, you're dynamically allocating arrays and pointing your variables at the first element in the array but you don't seem to have any mechanism to detect the size of the array. You need a method to track the size of the array so you don't accidentally cause a segmentation fault by going off the end of the array. (Three common methods: 1) track the number of elements allocated. 2) Mark the end of the array with some marker value and check for if, 3) hard-code it. All have trade-offs)
Fifth. You want to store the original value returned by malloc
so you can free
it at some point - if you modify what vfrom
points to you'll lose that original address and cause memory leaks.