Write a C program to create a Student record structure to store, N records, each record having the
structure shown below: USN, Student Name, and Semester. Write the necessary functions To display
all the records in the file and a function to search for a specific record based on the USN. In case the
record is not found, suitable message should be displayed. Both the options in this case must be
demonstrated.( Use pointer structure for dynamic memory allocation) .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char USN[15];
char name[100];
int semester;
};
void display(struct Student *students, int n) {
printf("\nStudent Records:\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < n; i++) {
printf("USN: %s\n", students[i].USN);
printf("Name: %s\n", students[i].name);
printf("Semester: %d\n", students[i].semester);
printf("-------------------------------------------------\n");
void search(struct Student *students, int n, char *usn) {
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(students[i].USN, usn) == 0) {
printf("\nRecord Found:\n");
printf("USN: %s\n", students[i].USN);
printf("Name: %s\n", students[i].name);
printf("Semester: %d\n", students[i].semester);
found = 1;
break;
if (!found) {
printf("\nRecord with USN %s not found.\n", usn);
int main() {
int N,ch;
printf("Enter the number of students: ");
scanf("%d", &N);
struct Student *students = (struct Student *)malloc(N * sizeof(struct Student));
if (students == NULL) {
printf("Memory allocation failed!\n");
return 1;
for (int i = 0; i < N; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Enter USN: ");
scanf("%s", students[i].USN);
printf("Enter Name: ");
scanf("%s", students[i].name);
printf("Enter Semester: ");
scanf("%d", &students[i].semester);
while(1){
printf("\n1. Display");
printf("\n2.Search");
printf("\n3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1: display(students, N);
break;
case 2: char searchUSN[15];
printf("\nEnter USN to search for: ");
scanf("%s", searchUSN);
search(students, N, searchUSN);
break;
case 3: exit(0);
free(students);
return 0;