✓ Link copied to clipboard
C 📅 July 13, 2026 ⏱ 5 min read

C Program to Store Data in Structures Dynamically

C Program to Store Data in Structures Dynamically

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 scanf and printf
  • 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.

main.cC▶ Run
#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.

▶ Open Compiler

Output of the Store Data in Structures Program

Here's what happens when you run the program with an input of 2 students.

Output
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
Output of C Program to Store Data in Structures Dynamically
Output of C Program to Store Data in Structures Dynamically

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.

  1. Read the number of students.
  2. Allocate memory for that number of Student structures using malloc.
  3. For each student, input their name and age.
  4. Display the data stored in each Student structure.
  5. 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])
Flowchart: the algorithm step by step

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;: Declares n for the number of students and i for loop iteration.
  • struct Student *students;: Declares a pointer to a dynamic array of Student.
  • scanf("%d", &n);: Reads the number of students.
  • malloc(n * sizeof(struct Student));: Allocates memory for n students and assigns it to students.
  • for (i = 0; i < n; i++): Iterates through each student to input and store data.
  • printf and scanf: 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 answer

Why do we use dynamic memory allocation in this program?

🎉Correct! Dynamic memory allocation allows handling of varying data sizes efficiently.
🤔Not quite. Think about how memory usage changes with different input sizes.

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.

Using structures with dynamic memory helps you manage complex data types on the fly.
If memory isn't freed, it can cause memory leaks, making programs consume more memory and crash eventually.
Yeah, you can expand the Student structure to add more fields and tweak the input/output code as needed.
Nope, besides malloc, you can also use calloc and realloc for dynamic memory allocation in C.
You can check if malloc gives you NULL. If it does, that means memory allocation failed.
Sure, you can use realloc to change the size of the already allocated memory block.

If you found this program helpful, check out these related programs:

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.