C StructuresLesson 37Free preview
C Arrays of Structs
This lesson is part of C · All levels
Summary
An array of struct is a collection of data of the same type that is stored side by side in memory, so it is suitable for data such as a list of students or a collection of coordinates of dots. Each element is accessed with an array index and then followed by a dot operator, e.g. students[0].nama. This structure makes it easier to process data through a loop because all the elements are stored in one neat memory block.
Key points
- Declared as a regular array, e.g. struct Side arr[10]; then each element contains one copy of the complete struct.
- Access uses arr[i].field; if the data is through a pointer use ptr->field.
- The total size of the array is equal to sizeof(struct) times the number of elements, so note also the padding between members.
- It can be filled directly with a nested initializer like {{1,2},{3,4}}.
- Loop for is most commonly used to process the entire struct element of an array.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.