结构体数组应用举例

结构体数组其实就是先定义结构体,之后在需要用到结构体的地方定义结构体数组,格式: struct 结构体名  结构体数组名[10];

eg:struct STU stu[10];这就表示定义了一个STU类型的大小为10的数组stu

代码举例:

 1 //结构体数组
 2 #include <iostream>
 3 using namespace std;
 4 struct STU {
 5     char name[20];
 6     char sex[20];
 7     char num[20];//学号
 8     int age;
 9 };
10 int main() {
11     struct STU stu[5];
12     int i;
13     for (i = 0; i < 5; i++) {
14         cout << "请输入第" << i + 1 << "个学生信息" << endl;
15         cout << "姓名:";
16         cin >> stu[i].name;
17         cout << "性别:";
18         cin >> stu[i].sex;
19         cout << "学号:";
20         cin >> stu[i].num;
21         cout << "年龄:";
22         cin >> stu[i].age;
23     }
24     cout << "请问要查询第几名学生的信息:";
25     cin >> i;
26     i = i + 1;
27     cout << "姓名:";
28     cout << stu[i].name << endl;
29     cout << "性别:";
30     cout << stu[i].sex << endl;
31     cout << "学号:";
32     cout << stu[i].num << endl;
33     cout << "年龄:";
34     cout << stu[i].age << endl;
35     return 0;
36 }
原文地址:https://www.cnblogs.com/2019-12-10-18ykx/p/12950764.html