Array of Structure


Explanation

An array of structure is used to store multiple records of the same structure type. Instead of creating separate structure variables for each record, we can create an array of structures to store many records together.

In this program, an array of structure b[5] is created to store details of multiple books. Each book contains information such as book name, number of pages, and book rate.

How It Works?

  • Step 1: Create a structure named Book with members name, pages, and rate.
  • Step 2: Create an array of structure b[5] to store multiple book records.
  • Step 3: Use a for loop to accept details of multiple books.
  • Step 4: Store each book record using structure array indexing.
  • Step 5: Use another for loop to display all stored book records in table format.

Example Program


#include <stdio.h>
#include <conio.h>

struct Book
{

    char name[20];

    int pages, rate;

};


main()
{

    struct Book b[5];

    int k;

    clrscr();


    for(k = 0; k <= 2; k++)

    {

        printf("Enter the Book Name : ");

        scanf("%s", b[k].name);


        printf("\nEnter the Book Page : ");

        scanf("%d", &b[k].pages);


        printf("\nEnter the Rate of Book : ");

        scanf("%d", &b[k].rate);

    }


    printf("\tName \tPages \tRate");


    for(k = 0; k <= 2; k++)

    {

        printf("\n%s", b[k].name);

        printf("\t%d", b[k].pages);

        printf("\t%d", b[k].rate);

    }


    getch();

}

Output

Enter the Book Name : C
Enter the Book Page : 150
Enter the Rate of Book : 250

Enter the Book Name : CPlusPlus
Enter the Book Page : 200
Enter the Rate of Book : 400

Enter the Book Name : Java
Enter the Book Page : 300
Enter the Rate of Book : 500

  Name    Pages   Rate
C         150     250
CPlusPlus  200     400
Java       300     500

Practice Exercises

Task 1: Write a C program using an array of structure to store and display details of 5 students. Store student name, roll number, and marks for each student.

Goal: Learn how to use an array of structures to store multiple records and display them using a loop.

💡 Hint: Create a structure named student with members name, rollno, and marks. Create an array of structure s[5]. Use a for loop to accept and display multiple student records.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];

    int rollno, marks;

};


main()
{

    struct student s[5];

    int k;

    clrscr();


    for(k = 0; k <= 4; k++)

    {

        printf("Enter Student Name : ");

        scanf("%s", s[k].name);


        printf("\nEnter Roll No : ");

        scanf("%d", &s[k].rollno);


        printf("\nEnter Marks : ");

        scanf("%d", &s[k].marks);

    }


    printf("\nName \tRoll No \tMarks");


    for(k = 0; k <= 4; k++)

    {

        printf("\n%s", s[k].name);

        printf("\t%d", s[k].rollno);

        printf("\t%d", s[k].marks);

    }


    getch();

}

Output
Enter Student Name : Amit
Enter Roll No : 1
Enter Marks : 80

Enter Student Name : Rahul
Enter Roll No : 2
Enter Marks : 75

Enter Student Name : Neha
Enter Roll No : 3
Enter Marks : 90

Name    Roll No   Marks
Amit     1        80
Rahul    2        75
Neha     3        90
Explanation: The program uses an array of structure to store multiple student records. Each structure element stores one student's name, roll number, and marks. A for loop is used to accept and display all student details.