You need to use the arrow ->
operator when you have a pointer to a struct (or union) on the left, and the dot .
operator when you have a struct on the left. It doesn't depend on what's on the right.
If person
was a pointer to a single Person
, to access its field, you'd use person->name
and person->age
.
But here person
is evidently a pointer to the first element of an array of Person
. (A pointer to the first element is how arrays are passed around in C.) To access an individual element of the array, you use the subscript operator (square brackets […]
): person[i]
. person[i]
is the element with index i
, not a pointer to the element with index i
. Since person[i]
is a struct, you use the dot operator to access its fields.
In addition, as others have already remarked, free(person);
inside the loop doesn't make sense. You'd free the whole array as soon as you've processed the first element. Call free(person)
after the loop, once you've finished cleaning up all the elements of the array (assuming that person
was allocated with malloc
).