第六阶段—typedef关键字详解:定义一个结构体数组,for循环按照分数从小到大排序,并打印出学生信息

 1 #include<stdio.h>
 2 typedef struct{
 3     char name[20];
 4     int id;
 5     int score;
 6 }student; 
 7     
 8 student st[3] = {
 9     {"jack",1,100},
10         {"rose",2,80},
11             {"shen",3,70}
12 };
13 student *p = st;
14 
15 void sort_student(student *p,int n)//更换变量的值,形参要用指针
16 {
17     int i = 0,j = 0;
18     student    temp;
19     for(i = 0; i <n-1; i++)//总体循环3次
20     {
21         for(j = 0; j < n-1-i; j++)//每次循环选出最大值,放在数组最后
22         {
23             if(p[j].score > p[j +1].score)//表示分数的变量p[j].score
24             {
25                 temp = p[j];
26                 p[j] = p[j+1];
27                 p[j +1] = temp;            
28             }
29         }
30     }
31     return;
32 }
33 
34 void output_student(student s)
35 {
36     printf("name\tid\tscore\t\n");
37     printf("%s\t%d\t%d\t\n",s.name, s.id, s.score);
38     
39     // printf("name:%s\n",s.name);
40     // printf("id:%d\n",s.id);
41     // printf("score:%d\n", s.score);
42     return;
43 }
44 void out_student(student *p)
45 {
46     int i;
47     for(i = 0; i < 3; i++)
48     {
49         output_student(p[i]);//输出变量的形式是p[i]
50     }
51     
52     return;
53 }
54 
55 int main(int argc, const char *argv[])
56 {
57     //input_student(st);
58     sort_student(p,3);
59     out_student(st);
60     
61     return 0;
62 }

 

posted @ 2021-10-02 22:29  major825  阅读(239)  评论(0)    收藏  举报