Think First ⚡
Before We Begin — A Quick Challenge!
You are building a Student Management System. You need to store a student's name, roll number, and marks. What is the problem with using separate variables?
💡 Challenge: Store info for 100 students
What is the smartest way to keep a student's info in a single unit?
3 separate arrays — name[], roll[], marks[]
struct — group everything into a custom data type
Just create one large char array
Use pointers for every variable
✓ Correct answer! A
struct is a custom data type that groups values of different types together — like a record where name + roll + marks are all in one place!
problem.cThe Nightmare 😱
// For just one student — it's fine
char name1[50]; int roll1; float marks1;
// For 100 students? Nightmare! 😭
char name1[50], name2[50], name3[50]...
int roll1, roll2, roll3...
float marks1, marks2, marks3...
// What is a better solution?
Definition
What is a Structure?
A structure is a user-defined data type in C that groups variables of different data types under a single name. Hover on highlighted code to explore each part!
struct_definition.cHover to explore ✶
struct Student {
char name[50]; // string member
int roll; // integer member
float marks; // float member
}; ← Semicolon mandatory!
// Declare a variable of type Student
struct Student s1;
🧠 Core Memory Rule:
A struct definition DOES NOT allocate memory — it's just a template. Memory is allocated only when you write
struct Student s1;!
⚠️ Common Mistake: Forgetting the
; after a struct definition — this causes a compiler error!Syntax Deep Dive
Declaration, Initialization & Member Access
Struct is defined — now declare variables, initialize them, and access members using the dot operator (.)!
init_access.cStep through ▶
struct Student {
char name[50];
int roll;
float marks;
};
// Method 1: Direct init
struct Student s1 = {"Abhay", 101, 95.5};
// Method 2: Assign
struct Student s2;
strcpy(s2.name, "Rohan");
s2.roll = 102;
s2.marks = 88.0;
// Access members
printf("%s", s1.name);
Step 1 of 5
📋
struct Blueprint
First, we define struct Student — 3 members: name, roll, marks. No memory is allocated yet.
💡 Dot Operator (.)
e.g.,
e.g.,
variable_name.member_namee.g.,
s1.roll gives 101e.g.,
s2.marks gives 88.0Special case for Strings:
s2.name = \"Rohan\" DOES NOT work directly! You must use strcpy() for strings.Interactive WOW ⭐
Live Player Cards with struct!
Type values directly into the code below to see the struct memory update the cards in real-time!
player_cards.cLive Editor ✏️
struct Player {
char name[50];
char position[10];
int jersey;
int goals;
float rating;
};
struct Player p1, p2;
// --- Initialize p1 (Player 1) ---
strcpy(p1.name, "");
strcpy(p1.position, "");
p1.jersey = ;
p1.goals = ;
p1.rating = f;
// --- Initialize p2 (Player 2) ---
strcpy(p2.name, "");
strcpy(p2.position, "");
p2.jersey = ;
p2.goals = ;
p2.rating = f;
94
RW
MESSI
JERSEY 10
GOALS 821
93
ST
RONALDO
JERSEY 7
GOALS 873
Array of Structures
struct Student cls[5] — The Entire Class!
Just like int arr[5] holds 5 integers, struct Student cls[5] holds 5 complete student records! Click on any student card.
array_of_structs.cClick a card →
struct Student {
char name[50];
int roll;
float marks;
};
struct Student cls[5] = {
{"Abhay", 101, 95.5},
{"Priya", 102, 88.0},
{"Rohan", 103, 76.5},
{"Sneha", 104, 91.0},
{"Arjun", 105, 83.5},
};
// Access: cls[i].member
printf("%s", cls[?].name);
👆 Click a student card
Click on a card — that student will be highlighted in the code and their struct data will appear here.
Memory: cls[0] is followed by cls[1] — all are contiguous! Total = 5 × sizeof(Student) bytes.
Loop:
Loop:
for(i=0;i<5;i++) printf(cls[i].name);Examples on Structures
Structures in Action
Three real-world examples — step through them and see the output!
student_record.cStep ▶
📤 Output:
Remember: Use struct to model real-world entities — student, book, employee all in one place!
Knowledge Check 🧠
Test Your Understanding
Question 1 of 4
Loading...