Qn-11.c (1486B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 06 Mar 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program to explain, how an array of stucture can you defined and accessed. */ 9 10 #include <stdio.h> 11 #include <string.h> 12 13 // 1. Defining the Structure 14 struct Student { 15 int rollNo; 16 char name[50]; 17 float marks; 18 }; 19 20 int main() { 21 int n, i; 22 23 printf("Enter the number of students: "); 24 scanf("%d", &n); 25 26 // 2. Defining an Array of Structures 27 // This creates 'n' blocks of memory, each large enough to hold a Student 28 struct Student s[n]; 29 30 // 3. Accessing members to STORE data 31 for (i = 0; i < n; i++) { 32 printf("\nEnter details for Student %d:\n", i + 1); 33 printf("Roll No: "); 34 scanf("%d", &s[i].rollNo); // Using dot (.) operator with index [i] 35 36 printf("Name: "); 37 getchar(); // To clear the newline character from buffer 38 fgets(s[i].name, sizeof(s[i].name), stdin); 39 s[i].name[strcspn(s[i].name, "\n")] = '\0'; // Safe newline removal 40 41 printf("Marks: "); 42 scanf("%f", &s[i].marks); 43 } 44 45 // 4. Accessing members to DISPLAY data 46 printf("\n--- Student Records ---\n"); 47 printf("ID\tName\t\tMarks\n"); 48 for (i = 0; i < n; i++) { 49 // Accessing using s[i].member 50 printf("%d\t%-15s\t%.2f\n", s[i].rollNo, s[i].name, s[i].marks); 51 } 52 53 return 0; 54 }