我正在尝试编写简单的程序来为一定数量的学生收集数据并最终输出。在我为一名学生输入数据后,我的程序崩溃了。
这是代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Student Student;
struct Student{
char name[20];
char lastname[20];
int age;
};
main() {
int i;
int n;
scanf("%d",&n);
Student *pStudents = NULL;
pStudents = (Student*)malloc(n*sizeof(Student));
for(i=0;i<n;i++) {
printf("Enter the students name: \n");
scanf("%s",(pStudents+i)->name);
printf("Enter lastname: \n");
scanf("%s",(pStudents+i)->lastname);
printf("Enter age: \n");
scanf("%d",(pStudents+i)->age);
}
for(i=0;i<n;i++) {
printf("%s",(pStudents+i)->name);
printf("%s",(pStudents+i)->lastname);
printf("%d",(pStudents+i)->age);
}
}
提前致谢。