5. 结构体 5.1 定义 1234567891011struct student { string name; int age; int score; }s3; int main() { student s1; student s2 = {"李四", 19, 80}; } 5.2 结构体数组 1struct 结构体名 数组名[元素个数] = {{}, {}, {}, {}, {}}; 5.3 结构体指针 12345student s = { "李四", 19, 80 };student* p = &s;cout << p->age << endl;cout << p->name << endl;cout << p->score << endl; 5.4 结构体嵌套结构体 1234567struct teacher { int id; string name; int age; struct student stu; } 5.5 结构体做函数参数 5.6 const const防止误操作 1234void printStu(const student *s) { cout<<s->name<<endl; } c++ > c++ 黑马 #c++ 5. 结构体 http://binbo-zappy.github.io/2024/11/27/cpp/5-结构体/ 作者 Binbo 发布于 2024年11月27日 许可协议 6. 内存分区模型 上一篇 4. 指针 下一篇