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?
Book with members name, pages, and rate.b[5] to store multiple book records.for loop to accept details of multiple books.for loop to display all stored book records in table format.#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(); }
array of structure to store and display details of 5 students. Store student name, roll number, and marks for each student.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.
#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(); }
for loop is used to accept and display all student details.