首页 > 代码库 > c语言 结构体动态创建

c语言 结构体动态创建

 1 #include<stdio.h> 2 #include<malloc.h> 3 struct Student 4 { 5     int num; 6     int total; 7     char name[20]; 8     float score[3]; 9 };10 11 int main()12 {13     int N,i,j;14     printf("Please input N:");15     scanf("%d",&N);16     struct Student *stu = NULL;17     stu = (struct Student*)malloc(sizeof(struct Student)*N);18     for(i = 0; i < N; i++)19     {20         stu[i].total = 0;21         printf("Please input the No%d student‘s number:",i+1);22         scanf("%d",&stu[i].num);23         printf("Please input the No%d student‘s name:",i+1);24         scanf("%s",stu[i].name);25         for(j = 0; j < 3; j++)26         {27             stu[i].score[j] = 0.0;28             printf("Please input the No%d student‘ score of %d:",i+1,j+1);29             scanf("%f",&stu[i].score[j]);30                stu[i].total = stu[i].total + (int)stu[i].score[j];31         }32     }33     for(i = 0; i < N; i++)34     {35         if(stu[i].total >= 240)36         {37             printf("%d %s\n",stu[i].num,stu[i].name);38         }39     }40     for(i = 0; i < N; i++)41     {42         for(j = 0; j < 3; j++)43         {44             if(stu[i].score[j] < 60)45             {46                printf("%d %s %.2f\n",stu[i].num,stu[i].name,stu[i].score[j]);47             }48         }49     //    printf(" %d \n",stu[i].total);50     }51     free(stu);52     return 0;53 }

 

c语言 结构体动态创建