Arrays of structures are one of the popular ways to store and process homogeneous data in C, like tables of data, arrays of points, employees, etc.
Background:
Support for arrays and structures appeared in the early versions of C for the convenience of organizing data. However, working with arrays of structures requires an understanding of the language's nuances, memory management, and data passing principles.
Problem:
Errors occur due to incorrect initialization of an array of structures, confusion with memory, passing an array to a function (it may be passed as a pointer), as well as access errors to structure fields through incorrect indexing.
Solution:
. and ->.Example code:
#include <stdio.h> struct Point { int x; int y; }; void print_points(struct Point *arr, int size) { for(int i = 0; i < size; ++i) { printf("(%d, %d)\n", arr[i].x, arr[i].y); } } int main() { struct Point points[3] = { {1,2}, {3,4}, {5,6} }; print_points(points, 3); return 0; }
Key features:
What is the difference between accessing structure fields in an array using dot and arrow notation?
arr[i].field is used when arr[i] is the structure itself.
ptr->field is used when ptr is a pointer to the structure.
struct Point *p = &points[0]; printf("%d", p->x); // correct // points[0].x — also correct
If partially initializing an array of structures, what values will the other fields have?
When partially initialized, unspecified fields are filled with zeros in statically allocated arrays, but not for automatic (stack) variables without initialization.
struct Point arr[2] = { {10} }; // arr[0].x = 10, arr[0].y = 0, arr[1].x and arr[1].y = 0
Are copies of structures made when passing an array of structures to a function?
No, a pointer to the first element of the array is passed, and the function can modify the element(s) since it operates on the original memory.
A programmer declared an array of structures without initializing the fields and passed it to a function for filling. They used the dot operator instead of the arrow operator when working with a pointer. As a result, a type error occurred and garbage values were used.
Pros:
Cons:
An explicit zero initializer {0} was used for the whole array, the function accepted a pointer and size, and access to the fields was strictly adhered to per type (arr[i].x).
Pros:
Cons: