结构体-结构体数组

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 // 结构体数组
 6 
 7 struct Student   // 1、定义结构体
 8 {
 9     string name; // 姓名
10     int age;     // 年龄
11     int score;   // 分数
12 };
13 
14 int main() {
15     struct Student stuArray[3] =  // 创建结构体数组
16     {
17         {"张三",18,100},
18         {"李四",28,99},
19         {"王五",38,66}
20     };
21 
22     stuArray[2].name = "赵六";    // 给结构体数组中的元素赋值
23     stuArray[2].age = 80;
24     stuArray[2].score = 60;
25 
26     for (int i = 0; i < 3; i++)
27     {
28         cout << "姓名: " << stuArray[i].name
29             << " 年龄: " << stuArray[i].age
30             << " 分数: " << stuArray[i].score << endl;
31     }
32 }

程序输出结果:

 

posted @ 2020-05-17 17:20  萧竹影丶  阅读(1256)  评论(0编辑  收藏  举报