程序代码:
/* 动态输入学生个数,并输入姓名,学号,和一门成绩! */ #include "stdio.h" #include "stdlib.h" struct STUDENT { int number; char name[15]; int score; }; int main() { int i = 0, n; printf("Hello World!\n"); struct STUDENT *p;//这要不要用*p=NULL printf("Please enter n:"); scanf("%d", &n); p=(struct STUDENT *)malloc(sizeof(struct STUDENT)*n); if(p==NULL) { printf("No enough memory!\n"); exit(0); } for(i=0; i < n; i++) { printf("Please enter the student's number:"); scanf("%d", &(p+i)->number); printf("Please enter the student's name:"); scanf("%s", &(p+i)->name); printf("Please enter the student's score:"); scanf("%d", &(p+i)->score); } for(i=0; i < n; i++) { printf("姓名:%s\n", (p+i)->name); printf("学号:%d\n", (p+i)->number); printf("成绩:%d\n", (p+i)->score); } free(p); return 0; }