-
Hajipur, Bihar, 844101
In C programming, structures are a way to group different types of variables under a single name. Unlike arrays, which store elements of the same type, structures can combine integers, floats, characters, and even other structures in one unit. This makes them ideal for representing real-world entities, such as students, employees, or products, where each entity has multiple attributes.
Structures improve code organization, readability, and modularity by logically grouping related data together. They allow programmers to pass complex data to functions, store multiple records, and handle entities more efficiently compared to using separate variables for each attribute.
To define a structure, use the struct keyword followed by a structure tag and member definitions:
struct Student {
char name[50];
int age;
float marks;
};
Student is the structure tag.
name, age, and marks are members.
Members can be of different types.
Note: Defining a structure does not allocate memory. Memory is allocated only when you declare structure variables.
After defining a structure, declare variables like this:
struct Student s1, s2;
s1 and s2 are structure variables.
Each variable has its own copy of the members.
You can also combine definition and declaration:
struct Student {
char name[50];
int age;
float marks;
} s1, s2;
This is particularly useful for small programs where the structure is only used for a limited number of variables.
Use the dot operator (.) to access or assign values to members:
s1.age = 20;
strcpy(s1.name, "Alice");
s1.marks = 88.5;
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
Use strcpy() to assign strings to character arrays.
Accessing each member individually allows fine control over data.
You can also perform operations on members directly:
s1.marks += 5; // increase marks by 5
Structures can contain other structures as members, which is useful for complex entities:
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int age;
struct Date dob; // nested structure
};
Nested structures allow you to group related data hierarchically.
Access nested members with dot notation:
s1.dob.day = 15;
s1.dob.month = 10;
s1.dob.year = 2005;
This approach is very useful for real-world applications, such as storing a student’s full profile with multiple nested attributes.
You can have arrays of structures to store multiple records:
struct Student students[3];
for(int i = 0; i < 3; i++) {
printf("Enter name: ");
scanf("%s", students[i].name);
printf("Enter age: ");
scanf("%d", &students[i].age);
printf("Enter marks: ");
scanf("%f", &students[i].marks);
}
Arrays of structures allow efficient management of multiple entities.
You can loop through the array to display or process data:
for(int i = 0; i < 3; i++) {
printf("%s, %d, %.2f\n", students[i].name, students[i].age, students[i].marks);
}
You can also use pointers to structures for dynamic memory management and function passing:
struct Student *ptr;
ptr = &s1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Marks: %.2f\n", ptr->marks);
Use the arrow operator (->) with structure pointers.
Pointers allow efficient data handling when passing structures to functions, especially large ones.
Structures can be passed to functions either by value or by reference:
By value:
void printStudent(struct Student s) {
printf("%s, %d, %.2f\n", s.name, s.age, s.marks);
}
By reference (using pointer):
void updateMarks(struct Student *s, float m) {
s->marks = m;
}
Passing by reference is more memory efficient.
Functions can modify structure members directly when passed by pointer.
This technique is used in real-world programs for updating records.
The typedef keyword simplifies structure usage:
typedef struct {
char name[50];
int age;
float marks;
} Student;
Student s1; // no need to write 'struct Student'
Makes code shorter and more readable.
Common in large programs or libraries for cleaner syntax.
#include <stdio.h>
#include <string.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int age;
float marks;
struct Date dob;
};
int main() {
struct Student s1;
strcpy(s1.name, "Alice");
s1.age = 20;
s1.marks = 89.5;
s1.dob.day = 15;
s1.dob.month = 10;
s1.dob.year = 2005;
printf("Student Details:\n");
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
printf("DOB: %02d-%02d-%d\n", s1.dob.day, s1.dob.month, s1.dob.year);
return 0;
}
Demonstrates nested structures, assignment, and printing.
Can be extended to arrays or pointers for advanced operations.
Structures in C allow grouping variables of different types into a single unit, making programs more organized, readable, and modular. They are fundamental for handling complex data and are widely used in student records, employee databases, inventory systems, and real-world applications. Mastery of structures, along with pointers, arrays, and functions, is essential for efficient and professional C programming.
Define a structure Employee with members name, id, salary, and department. Initialize a variable and print its details.
Create an array of 5 Student structures with name, age, and marks. Input data from the user and display all records.
Define a structure Date and another structure Event that contains name, location, and Date. Initialize and print the event details.
Write a function that takes a Student structure as a parameter and prints the student’s information.
Write a function that takes a pointer to an Employee structure and updates the employee’s salary.
Define a structure Book with members title, author, pages, and price. Create two book records and display them.
Create a nested structure for Address inside a Person structure and print the person’s full details.
Create an array of 3 Product structures with members name, id, and price. Find and display the product with the highest price.
Write a program using typedef to define a structure Car with members brand, model, and year. Create and print a car record.
Write a program to input data for 5 students, store it in an array of structures, and calculate the average marks of all students.