An array of structures is a structure in which each element of an exponential group is a structural body. In practical applications, structure arrays are often used to denote a group with the same data structure, such as a class student, a workshop worker, etc.
To define the structure body array and define the structure variables in a similar way, see the following example:
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//group
float score;//score
}CL ASS[5];
It means that there are 5 students in one class.
An array of structs can also be initialized when defined, for example:
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//group
float score;//score
}class[5] = {
"Li ping", 5,, ' C ', 145.0},
{"Zhang ping", 4,, ' A ', 130.5},
{"He fang", 1, ' a ', 1 48.5},
{"Cheng Ling", 2, ' F ', 139.0},
{"Wang Ming", 3, E, ' B ', 144.5}
};
When all the elements in an array are assigned, the length of the array is not given, for example:
struct stu{
char *name;//name
int num;//School number
int age;//ages
char Group;//group
float score;//score
}CL Ass[] = {
"Li ping", 5,, ' C ', 145.0},
{"Zhang ping", 4,, ' A ', 130.5},
{"He fang", 1, ' a ', 148.5},
{"Cheng Ling", 2, ' F ', 139.0},
{"Wang Ming", 3,, ' B ', 144.5}
};
The use of structure arrays is also simple, for example, to get the results of Wang Ming:
Class[4].score;
To modify Li Ping's Study group:
Class[0].group = ' B ';
The example calculates the total score of the class, the average score, and the number of people below 140 points.
#include <stdio.h>
struct{
char *name;//name
int num;//School number
int age;//ages
char Group;//Group
float score//Results
} Class[] = {
"Li ping", 5,, ' C ', 145.0},
{"Zhang ping", 4,, ' A ', 130.5},
{"He fang", 1, ' A ', 148.5} ,
{"Cheng Ling", 2, ' F ', 139.0},
{"Wang Ming", 3,, ' B ', 144.5}
};
int main () {
int i, num_140 = 0;
float average, sum = 0;
For (i=0 i<5; i++) {
sum = Class[i].score;
if (Class[i].score < 140) num_140++;
}
printf ("sum=%.2f\naverage=%.2f\nnum_140=%d\n", Sum, SUM/5, num_140);
return 0;
}
Run Result:
sum=707.50
average=141.50
num_140=2
The above is the C language structure array of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!