1

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?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
yoad w
  • 315
  • 3
  • 10

2 Answers2

4

In your code, poly_array is of type struct polygon*, so, poly_array[i] will be of type struct polygon. Thus, you have to use the . operator.

same logic applies for point_list[j] also.

To elaborate, quoting C11, chapter §6.5.2.1, Array subscripting (emphasis mine)

syntax

postfix-expression [ expression ]

Constraints

One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.

That said, please see this discussion on why not to cast the return value of malloc() and family in C..

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • so what i have is simply an array that holds actual polygons, yes? – yoad w Mar 08 '16 at 19:49
  • @yoadw Not really, pointers are not arrays. Concentrate on the data type itself. – Sourav Ghosh Mar 08 '16 at 19:51
  • so, if I wanted to have an array of pointers to polygons, how would I need to change my code? (I hope it's not too much to ask, thanks!) – yoad w Mar 08 '16 at 20:01
  • @yoadw array of pointers to polygons? It should be something like `struct polygon* poly_array[10]` and the `malloc()` will also change, but are you sure that's what you need? (10 is arbitrary anyway...) – Sourav Ghosh Mar 08 '16 at 20:03
  • i don't need it for any specific reason, i'm doing it to refresh memory\knowledge regarding C. this is actually a part of question from some old college test, and i'm messing around with the results. thanks. – yoad w Mar 08 '16 at 20:08
  • @yoadw You're welcome. You can also [consider accepting an answer that helped you](http://meta.stackexchange.com/q/5234/244062). – Sourav Ghosh Mar 08 '16 at 20:15
0

Short answer is referring to the array by index already dereferences the indexed item, so you are correct using dot syntax.

poly_array[i].point_list[j].x

You could as well use the "->" operator to dereference the item directly without using index operator []. In that case you would use pointer arithmetic.

(((poly_array+i)->point_list)+j)->x
arkadio
  • 1
  • 2