数组与结构 Created 2020-04-10 | Updated 2022-05-22
| Post Views:
初始化 结构与数组的初始化都要用 {} 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct name { char first_name[LEN]; char last_name[LEN]; }; struct student { struct name names ; float grade[NUM]; float average; }; struct student score [CSIZE ] = { {{"Zha" , "Yujie" }, {0 , 0 , 0 }, 0 }, {{"Jiang" , "Wenjie" }, {0 , 0 , 0 }, 0 }, {{"Liu" , "Chengming" }, {0 , 0 , 0 }, 0 }, {{"Pan" , "Xiangxiang" }, {0 , 0 , 0 }, 0 }
结构数组的传递 C能传递整个结构。但C不能传递整个数组 ,可以传递首元素地址,在函数中关联这个数组。
当遇到结构数组时, 要在函数中用地址关联这个数组,也只能传递结构数组的首元素的地址。 这个元素就是一个结构。
示例
数组名 = 数组的首元素地址jones = &jones[0]
注意是array of structures 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include <stdio.h> #define FUNDLEN 50 #define N 2 struct funds { char bank[FUNDLEN]; double bankfund; char save[FUNDLEN]; double savefund; }; double sum (const struct funds money[], int n) ;int main (void ) { struct funds jones [N ] = { { "Garlic-Melon Bank" , 4032.27 , "Lucky's Savings and Loan" , 8543.94 }, { "Honest Jack's Bank" , 3620.88 , "Party Time Savings" , 3802.91 } }; printf ("The Joneses have a total of $%.2f.\n" , sum(jones,N)); return 0 ; } double sum (const struct funds money[], int n) { double total; int i; for (i = 0 , total = 0 ; i < n; i++) total += money[i].bankfund + money[i].savefund; return (total); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <stdio.h> #define MON 12 struct year { char * m_name; int days; int m; }; int Days (int , struct year *, int *sum) ;int main () { struct year month [MON ]; char * name[12 ] = {"Jan" , "Feb" , "Mar" , "Ari" , "May" , "Jun" , "Jul" , "Aug" , "Set" , "Oct" , "Nov" , "Dem" }; int mon, sum; for (int i = 0 ; i < MON; i++) { month[i].m_name = name[i]; if (i % 2 == 0 ) month[i].days = 31 ; else month[i].days = 30 ; month[i].m = i + 1 ; } month[1 ].days = 28 ; printf ("Enter the month(else to quit): " ); while (scanf ("%d" , &mon)) { if (mon <= 12 && mon >= 1 ) { sum = 0 ; sum = Days(mon, month, &sum); printf ("%d\n" , sum); printf ("Enter the month again: " ); } else return (-1 ); } return 0 ; } int Days (int mon, struct year * month, int *sum) { for (int i = 0 ; i < mon; i++) { *sum += month[i].days; } return *sum; }
打印班级平均分 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include <stdio.h> #include <string.h> #define LEN 40 #define CSIZE 4 struct name { char fname[LEN]; char lname[LEN]; }; struct student { struct name n ; double grade[3 ]; double average; }; char * s_gets (char * st, int n) ;void get_grade (struct student *, char **) ;void get_average (struct student *) ;void print (struct student *) ;int main () { struct student person [CSIZE ] = { [0 ] = {"ferry" , "chan" }, [1 ] = {"faye" , "chan" }, [2 ] = {"tom" , "sion" }, [3 ] = {"jim" , "sion" } }; char *str[] = {"ferry chan" , "faye chan" , "tom sion" , "jim sion" }; get_grade(person, str); get_average(person); print(person); printf ("The average of class is %lf" , (person[0 ].average + person[1 ].average + person[2 ].average + person[3 ].average) / 4 ); return 0 ; } void get_grade (struct student *p, char **str) { int i; char fullname[LEN]; for (int n = 0 ; n < 4 ; n++) { int temp = 0 ; puts ("Enter stduent's fullname: " ); s_gets(fullname, LEN); for (i = 0 ; i < 4 ; i++) if (strcmp (fullname, str[i]) == 0 ) { temp = 1 ; break ; } if (temp) { for (int j = 0 ; j < 3 ; j++) { printf ("Enter your the %dth grade: " , j + 1 ); scanf ("%lf" , &(p[i].grade[j])); getchar(); } } } } void print (struct student *p) { for (int i = 0 ; i < 4 ; i++) { printf ("%s %s's grade is %lf %lf %lf and your average is %lf\n" , p[i].n.fname, p[i].n.lname, p[i].grade[0 ], p[i].grade[1 ], p[i].grade[2 ], p[i].average); } } void get_average (struct student * p) { for (int i = 0 ; i < 4 ; i++) { int sum = 0 ; for (int j = 0 ; j < 3 ; j++) { sum += p[i].grade[j]; } p[i].average = sum / 3 ; } } char * s_gets (char * st, int n) { char * find; char * ret_val; ret_val = fgets(st, n, stdin ); if (ret_val) { find = strchr (st, '\n' ); if (find) *find = '\0' ; else while (getchar() != '\n' ) continue ; } return ret_val; }