write a c program to allocate a dynamic memory for three student record scan it and print it using structure

17 Dec 2022 Balmiki Mandal 0 C Programming

C Program: Allocating Memory for Student Records using Structures

Write a C program to allocate dynamic memory for three student records, scan it, and print it using structures" means that you should create a program in the C programming language that does the following

  1. Defines a struct with fields for a student's name, age, and grade.
  2. Allocates dynamic memory for three instances of the struct using the malloc function or a related function.
  3. Prompts the user to input data for each of the three student records.
  4. Stores the data entered by the user into the allocated memory.
  5. Prints out the student records, including the name, age, and grade for each record, using the printf function or a related function.
  6. Frees the allocated memory using the free function or a related function.

In summary, the program should dynamically allocate memory for three student records, store data in the allocated memory, and then print out the data using structures.

C program to allocate dynamic memory for three student records, scan them, and print them using a structure:

C Programming
#include <stdio.h>
#include <stdlib.h>

typedef struct student {
  int roll_no;
  char name[20];
  float marks;
} Student;

int main() {
  // Allocate dynamic memory for three student records
  Student *students = malloc(sizeof(Student) * 3);

  // Scan the student records
  for (int i = 0; i < 3; i++) {
    printf("Enter student roll no.: ");
    scanf("%d", &students[i].roll_no);

    printf("Enter student name: ");
    scanf("%s", students[i].name);

    printf("Enter student marks: ");
    scanf("%f", &students[i].marks);
  }

  // Print the student records
  printf("\nStudent records:\n");
  for (int i = 0; i < 3; i++) {
    printf("Roll no.: %d\nName: %s\nMarks: %f\n\n", students[i].roll_no, students[i].name, students[i].marks);
  }

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

  return 0;
}
 This program first allocates dynamic memory for three student records using the malloc() function. Then, it scans the student records from the user using the scanf() function. Finally, it prints the student records to the console using the printf() function.
Example of the output of the program:
Enter student roll no.: 1
Enter student name: Alice
Enter student marks: 90.0

Enter student roll no.: 2
Enter student name: Bob
Enter student marks: 85.0

Enter student roll no.: 3
Enter student name: Charlie
Enter student marks: 75.0

Student records:

Roll no.: 1
Name: Alice
Marks: 90.0

Roll no.: 2
Name: Bob
Marks: 85.0

Roll no.: 3
Name: Charlie
Marks: 75.0

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • 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!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.