Storing data in structures dynamically is a powerful feature in C that allows for flexible memory management. This article will cover three different C Program to Store Data in Structures Dynamically. Each method will be explained in detail with example programs and outputs.
Prerequisites
Before diving into the examples, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with structures in C.
- Understanding of dynamic memory allocation in C.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Basic Dynamic Allocation
1.1 Explanation
In this approach, we use malloc
to allocate memory for a single structure dynamically. This is useful when the number of structures to be managed is known at runtime.
1.2 Program
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee *emp;
// Dynamically allocate memory for a single Employee structure
emp = (struct Employee*)malloc(sizeof(struct Employee));
if(emp == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Input employee data
printf("Enter name: ");
scanf("%s", emp->name);
printf("Enter age: ");
scanf("%d", &emp->age);
printf("Enter salary: ");
scanf("%f", &emp->salary);
// Display employee data
printf("Employee Details:\n");
printf("Name: %s\n", emp->name);
printf("Age: %d\n", emp->age);
printf("Salary: %.2f\n", emp->salary);
// Free allocated memory
free(emp);
return 0;
}
1.3 Output
Enter name: John
Enter age: 30
Enter salary: 50000
Employee Details:
Name: John
Age: 30
Salary: 50000.00
Solution 2: Array of Structures Using Dynamic Allocation
2.1 Explanation
In this approach, we dynamically allocate memory for an array of structures. This is useful when the number of structures to be managed is known at runtime and is greater than one.
2.2 Program
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee *emp;
int n, i;
printf("Enter the number of employees: ");
scanf("%d", &n);
// Dynamically allocate memory for an array of Employee structures
emp = (struct Employee*)malloc(n * sizeof(struct Employee));
if(emp == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Input employee data
for(i = 0; i < n; ++i) {
printf("Enter details for employee %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", emp[i].name);
printf("Enter age: ");
scanf("%d", &emp[i].age);
printf("Enter salary: ");
scanf("%f", &emp[i].salary);
}
// Display employee data
printf("Employee Details:\n");
for(i = 0; i < n; ++i) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Salary: %.2f\n", emp[i].salary);
}
// Free allocated memory
free(emp);
return 0;
}
Output
Enter the number of employees: 2
Enter details for employee 1:
Enter name: Alice
Enter age: 25
Enter salary: 45000
Enter details for employee 2:
Enter name: Bob
Enter age: 28
Enter salary: 48000
Employee Details:
Employee 1:
Name: Alice
Age: 25
Salary: 45000.00
Employee 2:
Name: Bob
Age: 28
Salary: 48000.00
Solution 3: Dynamic Allocation with Reallocation
3.1 Explanation
This approach involves dynamically allocating memory and then using realloc
to resize the memory block as needed. This is useful when the number of structures to be managed is not known at runtime and can change.
3.2 Program
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee *emp;
int n = 2, i = 0;
char choice;
// Initially allocate memory for 2 Employee structures
emp = (struct Employee*)malloc(n * sizeof(struct Employee));
if(emp == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
do {
if(i >= n) {
n *= 2;
emp = (struct Employee*)realloc(emp, n * sizeof(struct Employee));
if(emp == NULL) {
printf("Memory reallocation failed.\n");
return 1;
}
}
printf("Enter details for employee %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", emp[i].name);
printf("Enter age: ");
scanf("%d", &emp[i].age);
printf("Enter salary: ");
scanf("%f", &emp[i].salary);
++i;
printf("Do you want to add another employee (y/n)? ");
scanf(" %c", &choice);
} while(choice == 'y' || choice == 'Y');
// Display employee data
printf("Employee Details:\n");
for(int j = 0; j < i; ++j) {
printf("Employee %d:\n", j + 1);
printf("Name: %s\n", emp[j].name);
printf("Age: %d\n", emp[j].age);
printf("Salary: %.2f\n", emp[j].salary);
}
// Free allocated memory
free(emp);
return 0;
}
Output
Enter details for employee 1:
Enter name: Charlie
Enter age: 26
Enter salary: 47000
Do you want to add another employee (y/n)? y
Enter details for employee 2:
Enter name: Dave
Enter age: 30
Enter salary: 49000
Do you want to add another employee (y/n)? y
Enter details for employee 3:
Enter name: Eva
Enter age: 22
Enter salary: 41000
Do you want to add another employee (y/n)? n
Employee Details:
Employee 1:
Name: Charlie
Age: 26
Salary: 47000.00
Employee 2:
Name: Dave
Age: 30
Salary: 49000.00
Employee 3:
Name: Eva
Age: 22
Salary: 41000.00
Conclusion
Dynamic memory allocation in C allows for efficient and flexible management of memory when dealing with structures. This article presented three methods to store data in structures dynamically: basic dynamic allocation, dynamic allocation for an array of structures, and dynamic allocation with reallocation. Each method caters to different needs, from managing a single structure to handling an unknown number of structures. Understanding these techniques is crucial for effective memory management and optimization in C programming.