Structures
Learn how Structures allow C programs to group variables of different data types under one name and represent real-world entities.
Introduction
In the previous lessons, you learned that an array stores multiple values of the same data type. But consider a Student Management System. A student has different kinds of information: Roll Number (Integer), Name (String), Percentage (Float), and Grade (Character). Since these values have different data types, they cannot be stored in a single array. To solve this problem, C provides Structures.
What is a Structure?
A Structure is a user-defined data type that groups variables of different data types under a single name. Each variable inside a structure is called a Member. Structures help represent real-world entities such as students, employees, products, books, and vehicles.
Why Do We Need Structures?
Without Structures
With Structures
Real-World Analogy
Imagine an employee file. Instead of storing Employee ID, Name, Department, Salary, and Joining Date on separate pieces of paper, they are kept together in one file. Similarly, a structure groups related data together.
+----------------------+
| Employee ID |
| Employee Name |
| Department |
| Salary |
| Joining Date |
+----------------------+
All these details belong to ONE employee.Characteristics of Structures
- User-defined data type
- Can store different data types
- Groups related information
- Each member has its own memory
- Members are accessed individually
Structure Representation
Consider a Student structure. All the information belongs to one student:
Student
+----------------------+
| Roll Number : 101 |
| Name : Rahul |
| Percentage : 92.5 |
| Grade : A |
+----------------------+Members, Declaring, and Accessing
Each variable inside a structure is called a Member. A structure is declared using the struct keyword. The declaration defines the blueprint; no memory is allocated until a structure variable is created.
struct StructureName
{
data_type member1;
data_type member2;
};
// Creating a variable:
struct StructureName var1;
// Accessing members using the Dot (.) Operator:
var1.member1 = value;Example: Creating a Structure
#include <stdio.h>
struct Student
{
int rollNo;
char name[20];
float percentage;
char grade;
};
int main()
{
struct Student student1;
student1.rollNo = 101;
student1.percentage = 92.5;
student1.grade = 'A';
printf("Roll Number : %d\n", student1.rollNo);
printf("Percentage : %.1f\n", student1.percentage);
printf("Grade : %c", student1.grade);
return 0;
}Line-by-Line Explanation
Lines 3-9: struct Student { ... }
Defines a new structure named Student. It acts as a blueprint. rollNo stores the roll number, name stores the name with space for up to 19 characters plus the null character, percentage stores a floating-point value, and grade stores a character.
Line 12: struct Student student1;
Creates a structure variable named student1. Memory is now allocated for all its members.
Lines 14-16: Assigning Values
Uses the dot (.) operator to assign values to individual members, such as student1.rollNo = 101.
Lines 18-20: printf()
Displays the values stored in the structure members by accessing them with the dot operator.
Memory Representation of Example
student1
+------------------------+
| rollNo : 101 |
+------------------------+
| name : (empty) |
+------------------------+
| percentage : 92.5 |
+------------------------+
| grade : A |
+------------------------+
Each member has its own memory location.Program Execution Flow
Expected Output
Roll Number : 101
Percentage : 92.5
Grade : AAdvantages of Structures
- Group related data together
- Store different data types in one object
- Improve program readability
- Simplify data management
- Useful for representing real-world entities
- Easy to pass related information between functions
Real-World Applications
Student System
Roll Number, Name, Marks, and Grade.
Employee Management
Employee ID, Name, Department, and Salary.
Banking System
Account Number, Customer Name, and Balance.
Hospital Management
Patient ID, Name, Age, and Diagnosis.
E-Commerce
Product ID, Product Name, Price, and Quantity.
Structure vs Array
| Feature | Array | Structure |
|---|---|---|
| Data Types | Stores same data type | Stores different data types |
| Access | Elements accessed by index | Members accessed using dot (.) operator |
| Usage | Suitable for similar data | Suitable for related but different data |
Common Beginner Mistakes
Arrays store multiple values of the same type. Structures store related values of different types.
Structure members must be accessed using the dot operator, for example student1.rollNo.
A structure definition is only a blueprint. Memory is allocated only when a structure variable is created.
Choose member data types carefully based on the information they store. For example, use float for percentage instead of int.
Best Practices
- Use meaningful structure names such as Student and Employee.
- Group only related data together.
- Choose appropriate data types for members.
- Keep structures simple and focused.
- Use descriptive member names such as rollNo instead of r.
Frequently Asked Questions
What is a structure?
A structure is a user-defined data type that groups variables of different data types under one name.
Why are structures used?
Structures organize related information and simplify data management.
Can a structure contain different data types?
Yes. A structure can contain integers, characters, floating-point numbers, arrays, and even other structures.
How are structure members accessed?
Using the dot (.) operator.
Does defining a structure allocate memory?
No. Memory is allocated only when a structure variable is created.
Key Takeaways
- A structure is a user-defined data type.
- Structures group related variables of different data types.
- Each variable inside a structure is called a member.
- Structure members are accessed using the dot (.) operator.
- Structures are ideal for representing real-world entities such as students, employees, and products.
Summary
Structures are one of the most important user-defined data types in C because they allow programmers to group related information of different data types into a single unit. This makes programs more organized, readable, and easier to maintain. Structures are widely used in real-world applications to model complex entities such as students, employees, bank accounts, products, and many others.