Skip to study material
← Question index

QUESTION 06

Explain arrays of structures with a record-processing example.

5 marks

Answer in brief

An array of structures stores several records of the same kind in contiguous array storage.

Declaration

struct Student { int roll; int marks; };
struct Student students[3] = {{101, 70}, {102, 85}, {103, 80}};

Working

students[1] is the second complete record. students[1].marks selects its mark, 85. A loop from index 0 through 2 can process every record. Each structure element contains its own members; changing one record’s mark does not change another record.

Finding the best record

  1. Require a non-empty active list.
  2. Start best at index 0.
  3. Compare each later record’s marks with students[best].marks.
  4. Update best when a larger mark is found, then display the chosen record.

Example trace

For marks 70, 85 and 80, best starts at 0, changes to 1 on seeing 85 and remains 1 for 80. The chosen roll is 102. With a strict greater-than comparison, an equal later mark does not replace the first maximum.

Complexity and bounds

A single member access is O(1). Finding the largest mark scans n records and takes O(n) time with O(1) auxiliary space. The input records themselves require O(n) storage. Keep the active count within the actual array capacity; a structure member does not remove normal C array-bound rules.

Complete C example

main.c

Download main.c
/* CodeBhavya | Data Structures AR25 | Learning example */
#include <stdio.h>
struct Student { int roll, marks; };

int main(void)
{
    struct Student s[3] = {{101, 70}, {102, 85}, {103, 80}};
    int best = 0;
    for (int i = 1; i < 3; ++i) { if (s[i].marks > s[best].marks) { best = i; } }
    printf("Best roll = %d, marks = %d\n", s[best].roll, s[best].marks);
    return 0;
}

Sample input and output

The example uses values in the code.

Output

Best roll = 102, marks = 85