I haven't used C in a while, and now i'm trying to go back to it.
My problem is that my code actually works, although I was sure I will get syntax errors. He'res What I want to have: A dynamic array of polygons, each has a dynamic array of points.
struct point{
int x,y;
};
struct polygon{
int quantity;
struct point* point_list;
};
I will then initialize an array of polygons:
struct polygon* poly_array = (struct polygon*) malloc(sizeof(struct polygon)*num);
and also, initialize each polygon's point array:
poly_array[i].quantity = points;
poly_array[i].point_list = (struct point*) malloc (sizeof( struct point) * poly_array[i].quantity);
Now, what I thought I was doing, was create arrays of pointers to the object. So, to access inner fields, I would need to use the "->" operator. But no, it works with "direct" access to the fields (short version, without the loop code):
poly_array[i].point_list[j].x = i;
poly_array[i].point_list[j].y = j;
and also to print:
printf ("poly %d: (%d, %d)\n", j, poly_array[i].point_list[j].x, poly_array[i].point_list[j].y);
So, to make my question clear, I will ask again: since I'm iterating on pointers to objects, shouldn't I use the ->
operator?