C Online Compiler
Example: Array of Student Structures in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Array of Student Structures #include <stdio.h> #include <string.h> // For strcpy #define MAX_STUDENTS 3 int main() { // Step 1: Define the Student structure struct Student { int studentID; char name[50]; float gpa; }; // Step 2: Declare an array of Student structures struct Student students[MAX_STUDENTS]; // Step 3: Initialize the student records // Student 1 students[0].studentID = 101; strcpy(students[0].name, "Alice Green"); students[0].gpa = 3.75; // Student 2 students[1].studentID = 102; strcpy(students[1].name, "Bob Johnson"); students[1].gpa = 3.20; // Student 3 (using initializer list for brevity) struct Student temp = {103, "Charlie Brown", 3.99}; students[2] = temp; // Assign the temporary initialized struct // Step 4: Iterate through the array and print each student's details printf("--- All Student Details ---\n"); for (int i = 0; i < MAX_STUDENTS; i++) { printf("Student %d:\n", i + 1); printf(" ID: %d\n", students[i].studentID); printf(" Name: %s\n", students[i].name); printf(" GPA: %.2f\n", students[i].gpa); printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS