#include <iostream>
#include <string>
using namespace std;
int main(){
struct grade {string course; //定义一个内结构体
double score;
};
struct address{string province; //定义第二个内结构体
string city;
};
struct student{string name; //定义外结构体,包含两个内结构体
int age;
grade g; //内结构体需要定义一个变量
address a; //这里也有一个内结构体
};
struct student studentarr[]={
{"zhangsan",18,{"math",98},{"广西","南宁"}}, //这里的struct可以省略,需要使用“,”来分割,不要使用分号“;”
{"lishi",20,{"English",80},{"湖南"," 长沙"}},
{"wangwu",25,{"Chinese",89},{"湖北","武汉"}},
};
for (int i =0; i<3; i++){
student*p = &studentarr[i]; //创建一个指针指向数列内存地址
cout<<"Name:"<<p->name<<endl; //使用指针访问嵌套结构体变量的成员变量时,需要使用箭头运算符 ->
cout<<"Age:
用指针遍历结构体嵌套结构体
于 2023-03-04 21:07:58 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)