5. 结构体

5.1 定义

1
2
3
4
5
6
7
8
9
10
11
struct student
{
string name;
int age;
int score;
}s3;
int main()
{
student s1;
student s2 = {"李四", 19, 80};
}

5.2 结构体数组

1
struct 结构体名 数组名[元素个数] = {{}, {}, {}, {}, {}};

5.3 结构体指针

1
2
3
4
5
student s = { "李四", 19, 80 };
student* p = &s;
cout << p->age << endl;
cout << p->name << endl;
cout << p->score << endl;

5.4 结构体嵌套结构体

1
2
3
4
5
6
7
struct teacher
{
int id;
string name;
int age;
struct student stu;
}

5.5 结构体做函数参数

5.6 const

const防止误操作

1
2
3
4
void printStu(const student *s)
{
cout<<s->name<<endl;
}

5. 结构体
http://binbo-zappy.github.io/2024/11/27/cpp/5-结构体/
作者
Binbo
发布于
2024年11月27日
许可协议