C Online Compiler
Example: C Program to Store Information using Structures with Dynamic Memory Allocation
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C Program to Store Information using Structures with Dynamic Memory Allocation #include <stdio.h> #include <stdlib.h> // Make the structure of the student records struct students { char name[25]; char email[25]; int class; }; // It's the main function of the program int main() { struct students *pointer; int records_count; // Step-1 INPUT the no. of records printf("Enter the record count: "); scanf("%d", &records_count); // Step-2 Allocate the memories to the counted records by using the `malloc` function pointer = (struct students *)malloc(records_count * sizeof(struct students)); // Step-3 Iterate over the counted records to take the inputs from the user for (int i = 0; i < records_count; i++) { printf("\nEnter the student's information(Name, Email, and Class):\n"); scanf("%s %s %d", (pointer + i)->name, (pointer + i)->email, &(pointer + i)->class); } // Step-4 Final output of the program to display the student records printf("\nStudent's Information:\n"); printf("-----------------------------------------------------\n"); for (int i = 0; i < records_count; i++) { printf("%s\t%s\t%d\n", (pointer + i)->name, (pointer + i)->email, (pointer + i)->class); printf("-----------------------------------------------------\n"); } return 0; }
4 david david@yahoo.com 10 robert robert@yahoo.com 10 johny johny@email.com 12 sandy sandy@email.com 11
Output
Clear
ADVERTISEMENTS