原创 闫小林 C语言入门到精通 2020-12-30ios
收录于话题数组
#小林C++代码基础ide
95个函数
点击上方“C语言入门到精通”,选择置顶spa
第一时间关注程序猿身边的故事3d
做者blog
闫小林图片
白天搬砖,晚上作梦。我有故事,你有酒么?ci
C++结构体数组
it
C++结构体数组与之前介绍过的数值型数组的不一样之处在于:每一个数组元素都是一个结构体类 型的数据,它们都分别包括各个成员项。
C++结构体数组定义
C++结构体数组的定义和定义结构体变量的方法相仿,只需声明其为数组便可
struct Student{ //自定义结构体变量
int num;//学号
char sex;//性别
int age;//年龄
};
Student stu[5];//定义Student类型的结构体数组
struct Student{ //自定义结构体变量
int num;//学号
char sex;//性别
int age;//年龄
}stu[5];//定义Student类型的结构体数组
struct { //自定义结构体变量
int num;//学号
char sex;//性别
int age;//年龄
}stu[5];//定义Student类型的结构体数组
struct Student{ //自定义结构体变量
int num;//学号
char sex;//性别
int age;//年龄
}stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
#include<iostream>//预处理
using namespace std;//命名空间
int main()//主函数
{
struct Student{ //自定义结构体变量
int num;//学号
char sex;//性别
int age;//年龄
}stu[3]={{1001,'M',21},{1002,'F',18},{1003,'M',19}};
for(int i=0;i<3;i++)//循环输出结构体数组信息
{
cout<<stu[i].num<<endl;//输出学号
cout<<stu[i].sex<<endl;//输出性别
cout<<stu[i].age<<endl;//输出年龄
cout<<"---------"<<endl;//隔开
}
return 0; //函数返回值为0;
}
1001
M
21
---------
1002
F
18
---------
1003
M
19
---------
--------------------------------Process exited after 0.08727 seconds with return value 0请按任意键继续. . .