C语言创立一个结构体数组
时间: 2023-11-24 22:51:02 浏览: 113
以下是C语言创建结构体数组的示例代码:
```c
#include <stdio.h>
struct student {
char name[50];
int age;
float score;
};
int main() {
// 创建结构体数组
struct student stu_arr[3];
// 初始化结构体数组
strcpy(stu_arr[0].name, "Tom");
stu_arr[0].age = 18;
stu_arr[0].score = 90.5;
strcpy(stu_arr[1].name, "Jerry");
stu_arr[1].age = 19;
stu_arr[1].score = 88.5;
strcpy(stu_arr[2].name, "Mike");
stu_arr[2].age = 20;
stu_arr[2].score = 95.0;
// 遍历结构体数组并输出
for (int i = 0; i < 3; i++) {
printf("Name: %s, Age: %d, Score: %.1f\n", stu_arr[i].name, stu_arr[i].age, stu_arr[i].score);
}
return 0;
}
```
阅读全文