把结构体整体存入数组
时间: 2024-04-01 14:27:43 浏览: 78
将结构体保存到文件
可以将结构体整体存入数组,具体方法如下所示:
```c
// 定义结构体
struct Student {
char name[20];
int age;
float score;
};
int main() {
// 定义结构体数组
struct Student stu[3] = {
{"Tom", 18, 90.5},
{"Jerry", 19, 88.5},
{"Mike", 20, 85.0}
};
// 遍历结构体数组并输出每个结构体的信息
for (int i = 0; i < 3; i++) {
printf("Name: %s, Age: %d, Score: %.1f\n", stu[i].name, stu[i].age, stu[i].score);
}
return 0;
}
```
运行结果如下:
```
Name: Tom, Age: 18, Score: 90.5
Name: Jerry, Age: 19, Score: 88.5
Name: Mike, Age: 20, Score: 85.0
```
阅读全文