One structure variable holds one record. A class has many students, and a company has many employees. Combining structures with arrays gives a table of records, which is how simple databases are built in C.
Arrays of structures
Syntax
struct tag array-name[size];
struct student stud[20]; creates 20 student records, stud[0] to stud[19]. A member of one record is written index first, then dot: stud[i].roll_no. An array of structures can be initialised with nested braces, one inner group per record.
Layout of struct student stud[3]
roll_no name course marks_obtainedstud[0] 101 Asha MCA 84stud[1] 102 Ravi MCA 71stud[2] 103 Meena BCA 90
Arrays inside structures
A member can be an array, such as the marks in three subjects: int subject[3];. Combined with an array of structures, an element is reached as stud[i].subject[j]: student i, subject j.
Key takeaways
struct tag a[n]; creates n records, a[0] to a[n-1].
Access a field with index first, then dot: a[i].member.
A member may be an array: a[i].member[j].
Loops over the array process every record with the same code.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Declare a structure employee with a name and a monthly salary, create an array for 50 employees, and write a loop that computes the total salary of the first n employees.
Show worked answer
struct employee { char name[30]; float salary; }; then struct employee emp[50];. Loop: total = 0; for (i = 0; i < n; i++) total = total + emp[i].salary;. emp[i] selects the i-th record and .salary its salary field; starting total at 0 and running i from 0 to n − 1 adds each employee’s salary exactly once.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/structures-and-unions/structures-and-arrays/ to read this page.