MCS-201 · Unit 6

Structures and Unions

Section 4 of 5

Block 2 · Unit 6Structures and Unions9 min · Section 4 of 5

6.4Structures and Arrays

Covers IGNOU MCS-201 §6.6

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_obtained
stud[0]      101      Asha     MCA          84
stud[1]      102      Ravi     MCA          71
stud[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 1 · multiple choice

For struct student stud[20];, how is the roll number of the fifth student accessed?

Show answer

Answer: C. stud[4].roll_no

The fifth element has index 4, and the index is applied to the array before the dot selects the member.

Question 2 · multiple choice

If sizeof(struct student) is 48, how many bytes does struct student stud[20]; occupy?

Show answer

Answer: D. 960

An array of 20 elements occupies 20 × 48 = 960 bytes.

Question 3 · multiple choice

Given int subject[3]; inside the structure, what does stud[2].subject[0] refer to?

Show answer

Answer: A. The first subject mark of the third student

stud[2] is the third student and subject[0] is that student’s first mark.

Question 4 · multiple choice

What does this program print?

C · avg_struct.c
#include <stdio.h>

struct item { char name[10]; int qty; };

int main(void)
{
    struct item list[3] = { {"pen", 10}, {"ink", 4}, {"pad", 6} };
    int i, total = 0;
    for (i = 0; i < 3; i++)
        total += list[i].qty;
    printf("%d\n", total);
    return 0;
}
Show answer

Answer: B. 20

The loop adds the qty member of every record: 10 + 4 + 6 = 20.

Question 5 · multiple choice

Which conversion should be used to read stud[i].name, declared as char name[20]?

Show answer

Answer: D. scanf("%19s", stud[i].name);

A string member is read with %s (width 19 for a 20-byte array) and no &, because the array name is already an address.

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.

↑ ↓ move↵ openTab switch type