practice structure, pointers, and dynamic memory allocation Write a C program to
ID: 3549621 • Letter: P
Question
practice structure, pointers, and dynamic memory allocation
Write a C program to use structure, pointers, and dynamic memory allocation to store student id, name, and letter grade of 5 students. Sample output is as follows:
Enter no of Students :5
* * * * Enter students' info. * * * *
student ID-->1111
student name-->William
student grade-->A
student ID-->1234
student name-->Mary
student grade-->B
student ID-->5643
student name-->Kelly
student grade-->A
student ID-->9134
student name-->Eden
student grade-->C
student ID-->1012
student name-->Bob
student grade-->A
* * * * Entered students' info * * * *
1111 William A
1234 Mary B
5643 Kelly A
9134 Eden C
1012 Bob A
Explanation / Answer
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct person
{
int student_id;
char *name;
char *grade;
};
int main()
{
int n,i;
struct person *testst;
printf("Enter number of students ");
scanf("%d",&n);
testst=(struct person *)malloc(n*sizeof(*testst));
for(i=0;i<n;i++)
{
printf("student id=");
scanf("%d",&testst[i].student_id);
printf("student name=");
scanf("%s",&testst[i].name);
printf("student grade=");
scanf("%s",&testst[i].grade);
}
for(i=0;i<n;i++)
{
printf("%d ",testst[i].student_id);
printf("%s ",testst[i].name);
printf("%s ",testst[i].grade);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.