“If arrays are the shelves of data, structures are the well-organized cupboards!”
🌟 Introduction
In your C journey so far, you’ve mastered arrays, loops,
functions — all neat ways to handle data.
But what happens when one entity (say, a student, a car, or a bank
account) has different types of data tied together?
👉
One integer roll number, one float CGPA, one string name — and you want to keep
them together logically.
That’s where Structures walk in — the Avengers of C
data organization!
Each hero (member) has a different superpower (data type), but together they
save you from chaos.
🧩 What is a Structure?
A structure (or struct) is a user-defined data type
that lets you group variables of different types under one name.
Here’s the syntax:
struct structure_name {
data_type member1;
data_type member2;
...
};
For example:
struct Student {
int roll;
char name[30];
float marks;
};
You just told the compiler:
“Hey C, I’m designing my own data type called Student which
has an int, a string, and a float.”
🚀 Declaring and Using Structures
Once the structure is defined, you can create variables of
that type:
struct Student s1, s2;
Now each s1 and s2 can hold roll, name, and marks.
Let’s fill them up:
#include <stdio.h>#include <string.h>
struct Student { int roll; char name[30]; float marks;};
int main() { struct Student s1;
s1.roll = 101; strcpy(s1.name, "Aditi Sharma"); s1.marks = 94.5;
printf("Student Details:\n"); printf("Roll: %d\n", s1.roll); printf("Name: %s\n", s1.name); printf("Marks: %.2f\n", s1.marks);
return 0;}
🧠 Output
Student Details:
Roll: 101
Name: Aditi Sharma
Marks: 94.50
Simple, right? But very powerful
🕹️ Accessing Structure Members
You access each member using the dot (.) operator
when using a normal structure variable.
Example:
student1.roll, student1.name, student1.marks.
If you have a pointer to a structure, use the arrow
(->) operator.
(We’ll explore that in the next post.)
⚙️ Initializing Structures
Just like arrays, you can initialize structures directly
when declaring them:
struct Student s2 = {102, "Rohan Das", 88.0};
Or initialize later:
struct Student s3;
s3 = (struct Student){103, "Priya Sen", 91.5};
C gives you the freedom to mix and match!
#include <stdio.h>#include <string.h>
struct Student { int roll; char name[30]; float marks;};
int main() { struct Student students[3] = { {101, "Aditi", 94.5}, {102, "Rohan", 88.0}, {103, "Priya", 91.5} };
for (int i = 0; i < 3; i++) { printf("\nStudent %d\n", i + 1); printf("Roll: %d\n", students[i].roll); printf("Name: %s\n", students[i].name); printf("Marks: %.2f\n", students[i].marks); }
return 0;}
💡 Think of this as a
“mini database” — each structure is a record.
🧰 Structure Inside Structure (Nested Structures)
Structures can hold other structures too!
Example:
struct Date {
int day;
int month;
int year;
};
struct Student {
int roll;
char name[30];
struct Date
dob; // nested structure
float marks;
};
Now you can do:
struct Student s1 = {101, "Aditi", {5, 8, 2004},
94.5};
printf("DOB: %02d-%02d-%d", s1.dob.day, s1.dob.month, s1.dob.year);
🧭 Why Use Structures?
|
Situation |
Without Structures |
With Structures |
|
Student info |
3 separate variables per student |
1 structure per student |
|
Employee record |
Messy multiple arrays |
Clean and cohesive |
|
Real-world modeling |
Hard to manage |
Logically grouped |
Bottom line: Structures help your code speak object language even in a non-object-oriented world!
⚡ Quick Recap: Structure Superpowers
✅ Can group multiple data types
together
✅
Easy to manage, pass, and return from functions
✅
Great for arrays of records
✅
Basis for advanced concepts like Unions, Pointers to Structures,
and Files of Structures
💡 Try This Yourself
Mini Challenge:
Write a C program to store details (roll, name, marks) of 5 students and print the topper’s name and marks.Hint: Use an array of struct Student and a loop to
find max marks.
🧠 What’s Next?
In the next post — “Structures in Action — From Student
Records to Real-World Models”,
we’ll go hands-on with passing structures to functions, returning
structures, and using them like real-life blueprints.
Stay tuned — because this is where C starts feeling like mini
OOP! 😄
✍️ Written with love by the
ProgVeda Team — making C fun, one byte at a time!
Post a Comment