结构体可以理解为一个新的数据类型,里面可以定义新的内容,结构体就是大当家的!!!
struct Student {
//结构体
string name;
int age;
int score;
};
一个定义后的结构体有三种创建方式:
1、直接定义一个变量:struct teacher arr[2];
2、在定变量的时候,直接赋值:struct student stu = { "张三",12,99 };
3、在定义结构体的时候,就定义变量(不常见,不推荐!!!)
结构体打印
有两种方案:值传递、地址传递
值传递可以理解为直接复制传值,地址传递相当于快捷方式
void print_student(struct Student a) {
//传值是直接复制,传地址是建立快捷方式
cout << a.name << a.age << a.score << endl;
}
void prints_pointer_tudent(struct Student* a) {
cout << a->name << a->age << a->score << endl;
}
值传递用: 变量.属性,地址传递用: 变量->属性
结构体数组:
结构体数组和数组有一样的意思:
Student arr[3] = {
{
"张三",12,89},</