Storing data dynamically in C means using pointers and structures. Picture this: you’ve got a list of students, but you don't know how many you’ll need until your program runs. Dynamic memory allocation is your answer. Let’s see how to do this in C.
Prerequisites for the C Program to Store Data
Before diving into the code, ensure you're comfortable with these concepts:
- Structures in C
- Pointers and dynamic memory allocation using
malloc - Input and output functions like
scanfandprintf - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Store Data in Structures Dynamically
Here's the full code to dynamically store student data using structures.
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int age;
};
int main() {
int n, i;
struct Student *students;
// Ask user for the number of students
printf("Enter number of students: ");
scanf("%d", &n);
// Allocate memory for n students
students = (struct Student *)malloc(n * sizeof(struct Student));
// Input data for each student
for(i = 0; i < n; i++) {
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
}
// Display stored data
printf("\nStored Data:\n");
for(i = 0; i < n; i++) {
printf("Name: %s, Age: %d\n", students[i].name, students[i].age);
}
// Free allocated memory
free(students);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Store Data in Structures Program
Here's what happens when you run the program with an input of 2 students.
Enter number of students: 2
Enter name for student 1: John
Enter age for student 1: 20
Enter name for student 2: Jane
Enter age for student 2: 21
Stored Data:
Name: John, Age: 20
Name: Jane, Age: 21
Algorithm to Store Data Dynamically in C
Storing data in structures dynamically means managing memory and data at runtime. Here's the logic step-by-step.
- Read the number of students.
- Allocate memory for that number of
Studentstructures usingmalloc. - For each student, input their name and age.
- Display the data stored in each
Studentstructure. - Free the allocated memory.
flowchart TD
A([Start]) --> B{allocate memory using malloc?}
B -->|Yes| C[Initialize the structure]
C --> D[Input data for structure]
D --> E{More data?}
E -->|Yes| B
E -->|No| F[Display stored data]
F --> G[Free allocated memory]
G --> H([End])How the C Program to Store Data Works
Let's break down the code and see how each part works together.
#include <stdio.h>and#include <stdlib.h>: These headers bring in I/O functions and dynamic memory allocation capabilities.struct Student: Defines a structure to hold a student's name and age.int main(): The program's entry point, where execution begins.int n, i;: Declaresnfor the number of students andifor loop iteration.struct Student *students;: Declares a pointer to a dynamic array ofStudent.scanf("%d", &n);: Reads the number of students.malloc(n * sizeof(struct Student));: Allocates memory fornstudents and assigns it tostudents.for (i = 0; i < n; i++): Iterates through each student to input and store data.printfandscanf: Used to get input for each student's name and age.printf("\nStored Data:\n");: Starts displaying stored student data.printf("Name: %s, Age: %d\n", ...): Outputs each student's name and age.free(students);: Frees the allocated memory to prevent memory leaks.return 0;: Signals successful completion of the program.
Quick Quiz — test your understanding
Tap an option to check your answerWhy do we use dynamic memory allocation in this program?
Time and Space Complexity of the C Program to Store Data
Understanding the complexity is crucial for efficient programming.
Time Complexity: O(n). n here is the number of students. For each student, we perform a constant amount of work: reading and storing data. So if you input 5 students, the loop runs five times.
Space Complexity: O(n). We dynamically allocate space for n students. The memory for each Student structure grows with the number of entries.
Common mistake: Not freeing dynamic memory can cause memory leaks.
Key takeaway: Dynamic memory allocation lets you flexibly manage data size while your program runs.
FAQs About C Program to Store Data in Structures Dynamically
Here are answers to some common questions.
Student structure to add more fields and tweak the input/output code as needed.malloc, you can also use calloc and realloc for dynamic memory allocation in C.malloc gives you NULL. If it does, that means memory allocation failed.realloc to change the size of the already allocated memory block.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Store Information of Students Using Structure
- C Program to Add Two Distances in Inch-Feet Using Structures
- C Program to Swap Two Numbers Using Pointers
- C Program to Add Two Complex Numbers by Passing Structure
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
With dynamic memory, you can handle data sizes that aren’t known until runtime. Try changing the code to include extra student info like grades or addresses. This technique is super useful in lots of real-world apps.
Ready to test your skills? Check out the C programming quiz and play around with this program in the online compiler.