C Program to Store Data in Structures Dynamically

Storing data in structures dynamically typically involves dynamically allocating memory for the structures and their fields. Here's a C program that demonstrates how to store data in structures dynamically:

#include <stdio.h>

#include <stdlib.h>

// Define a structure to store student information
struct Student {
    int rollNumber;
    char name[50];
    int age;
    float marks;
};

int main() {
    int numStudents;

    printf("Enter the number of students: ");
    scanf("%d", &numStudents);

    // Dynamically allocate memory for an array of structures
    struct Student *students = (struct Student *)malloc(numStudents * sizeof(struct Student));

    if (students == NULL) {
        printf("Memory allocation failed. Exiting program.\n");
        return 1;
    }

    // Input student information
    for (int i = 0; i < numStudents; i++) {
        printf("\nEnter information for Student %d:\n", i + 1);
        printf("Enter Roll Number: ");
        scanf("%d", &(students[i].rollNumber));
        printf("Enter Name: ");
        scanf(" %[^\n]", students[i].name);
        printf("Enter Age: ");
        scanf("%d", &(students[i].age));
        printf("Enter Marks: ");
        scanf("%f", &(students[i].marks));
    }

    // Display student information
    printf("\nStudent Information:\n");
    for (int i = 0; i < numStudents; i++) {
        printf("Student %d\n", i + 1);
        printf("Roll Number: %d\n", students[i].rollNumber);
        printf("Name: %s\n", students[i].name);
        printf("Age: %d\n", students[i].age);
        printf("Marks: %.2f\n\n", students[i].marks);
    }

    // Free the dynamically allocated memory
    free(students);

    return 0;

In this program, we start by asking the user to enter the number of students they want to store information for. We then dynamically allocate memory for an array of Student structures using malloc. The malloc function allocates memory based on the size of the structure multiplied by the number of students entered by the user.

We also check if the memory allocation was successful. If memory allocation fails, the program prints an error message and exits.

The program proceeds to input student information just as in the previous example. Data is stored in the dynamically allocated memory.

Finally, the program displays the stored student information and frees the dynamically allocated memory using the free function to avoid memory leaks.

This program allows you to store data in structures dynamically by allocating memory at runtime.

Input and Output  

Enter the number of students: 2

Enter information for Student 1:
Enter Roll Number: 101
Enter Name: John Smith
Enter Age: 20
Enter Marks: 95.5

Enter information for Student 2:
Enter Roll Number: 102
Enter Name: Alice Johnson
Enter Age: 21
Enter Marks: 88.0

Student Information:
Student 1
Roll Number: 101
Name: John Smith
Age: 20
Marks: 95.50

Student 2
Roll Number: 102
Name: Alice Johnson
Age: 21
Marks: 88.00 

Explanation:

  1. The program starts by asking the user to input the number of students they want to store information for. In this example, the user enters 2 as the number of students.

  2. After the user input, the program dynamically allocates memory to store information for the specified number of students. In this case, it allocates memory for two Student structures.

  3. The program then proceeds to input student information for each student, as specified by the user. It asks for the roll number, name, age, and marks for each student. The user provides this information for both Student 1 and Student 2.

  4. After the user has provided all the required data, the program displays the stored student information. It shows the roll number, name, age, and marks for each student.

  5. Finally, the program frees the dynamically allocated memory using the free function to prevent memory leaks.

In this example, the user entered data for two students, and the program stored and displayed this information successfully. This program allows you to store and manage data for any number of students, as the memory allocation is done dynamically based on the user's input.

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post