write a c program Allocate a dynamic memory for n students record it scan it and print it using structure
C Program: Allocating Dynamic Memory for Student Records
To allocate dynamic memory for n student records using a structure in C, we can use the following steps:
- Declare a structure to represent a student record.
- Allocate an array of structures dynamically using the malloc() function.
- Scan the student records from the user and store them in the array of structures.
- Print the student records from the array of structures.
C program demonstrates the above steps:
C Programming
#include <stdio.h>
#include <stdlib.h>
// Define a structure to represent a student record.
struct student {
int roll_no;
char name[20];
int marks;
};
// Function to allocate dynamic memory for n student records.
struct student* allocate_memory(int n) {
struct student* students = (struct student*)malloc(n * sizeof(struct student));
if (students == NULL) {
printf("Error allocating memory!\n");
exit(1);
}
return students;
}
// Function to scan student records from the user and store them in the array of structures.
void scan_student_records(struct student* students, int n) {
for (int i = 0; i < n; i++) {
printf("Enter roll no. of student %d: ", i + 1);
scanf("%d", &students[i].roll_no);
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter marks of student %d: ", i + 1);
scanf("%d", &students[i].marks);
}
}
// Function to print student records from the array of structures.
void print_student_records(struct student* students, int n) {
printf("\nStudent records:\n");
printf("Roll No. | Name | Marks\n");
printf("------- | ---- | ------\n");
for (int i = 0; i < n; i++) {
printf("%d | %s | %d\n", students[i].roll_no, students[i].name, students[i].marks);
}
}
// Main function.
int main() {
int n;
printf("Enter the number of student records: ");
scanf("%d", &n);
// Allocate dynamic memory for n student records.
struct student* students = allocate_memory(n);
// Scan the student records from the user and store them in the array of structures.
scan_student_records(students, n);
// Print the student records from the array of structures.
print_student_records(students, n);
// Free the dynamically allocated memory.
free(students);
return 0;
}
Sample output:
Enter the number of student records: 2
Enter roll no. of student 1: 1
Enter name of student 1: John Doe
Enter marks of student 1: 90
Enter roll no. of student 2: 2
Enter name of student 2: Jane Doe
Enter marks of student 2: 85
Student records:
Roll No. | Name | Marks
------- | ---- | ------\
1 | John Doe | 90
2 | Jane Doe | 85
Further Reading:
Dynamic Memory allocation or Run time memory allocation in c
What is static memory allocation and dynamic memory allocation?
Write a c program to allocate dynamic memory for 5 integers scan it and print it
write a c program to allocate dynamic memory for n integers scan it and print n value takes it run times
write a c program to allocate dynamic memory for one string scan it print it
write a c program to allocate dynamic memory for three string scan it and print it
Write a c program to allocate a dynamic memory for n string
c program to allocate dynamic memory for 2D array integers where a number of rows and numbers of columns scan from the user at run time,
Write a c program to allocate a dynamic memory for 1 student record it and scan it and print it
write a c program to allocate a dynamic memory for three student record scan it and print it using structure
write a c program Allocate a dynamic memory for n students record it scan it and print it using structure
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [info@electro4u.net].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!