目录
1.1结构体基本概念
结构体属于用户自定义的数据类型
1.2结构体的定义和使用
语法:struct 结构体名{结构体成员列表};
结构体创建变量的方式:
1.struct 结构体名 变量名
2.struct 结构体名 变量名={成员1值,成员2值...}
3.定义结构体时顺便创建变量
注意:在创建变量时struct关键字可以省略
案例:1.创建学生的数据类型2.通过学生类型创建具体的学生
#include<iostream>
using namespace std;
#include<string>
//1.创建学生的数据类型
struct student{
//名字
string name;
//年龄
int age;
//成绩
int score;
}s3;
int main()
{
//2.创建变量
//1.
struct student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << " 姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//2.
struct student s2 = { "李四", 19, 80 };
cout << " 姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
//3.
s3.name = "王五";
s3.age = 20;
s3.score = 89;
cout << " 姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
system("pause");
return 0;
}
1.3结构体数组
语法:struct 结构体名 数组名【元素个数】={{},{},...{}};
int main()
{
struct student stuarr[3] = { { "z", 18, 90 }, { "t", 19, 100 }, { "w", 20, 97 } };
stuarr[2].name = "王五";
for (int i = 0; i < 3; i++)
{
cout << " 姓名:" << stuarr[i].name << " 年龄:" << stuarr[i].age << " 分数:" << stuarr[i].score << endl;
}
system("pause");
return 0;
}
结果如下:
1.4结构体指针
1.创建学生结构体变量
2.通过指针指向结构体变量
3.通过指针访问结构体变量中的数据
指针是通过->访问的
int main()
{
//struct可以省略
struct student s1= {"张三",18,100};
struct student *p = &s1;//s1返回的地址类型为struct student类型
cout << " 名字:" << p->name << " 年龄:" << p->age << " 成绩:" << p->score << endl;
system("pause");
return 0;
}
1.5结构体嵌套结构体
案例:每个老师辅导一个学员,一个老师的结构体中记录一个学生的结构体。
#include<iostream>
using namespace std;
#include<string>
//1.创建学生的数据类型
struct student{
//名字
string name;
//年龄
int age;
//成绩
int score;
};
//老师结构体定义
struct teacher {
//编号
int id;
//名字
string name;
//年龄
int age;
//学生
struct student stu;
};
int main()
{
struct teacher t= {03,"老王",25};
struct teacher *p=&t;
p->stu.name = "小王";
p->stu.age = 18;
p->stu.score = 90;
cout << " 老师姓名: " << p->name << " 老师编号: " << p->id << " 老师年龄: " << p->age << " 学生姓名: " <<
p->stu.name << " 学生年龄:" << p->stu.age << " 学生成绩:" << p->stu.score << endl;
system("pause");
return 0;
}
1.6结构体做函数参数
作用:将结构体作为参数向函数中传递。
1.值传递 2.地址传递
测试如下:
#include<iostream>
using namespace std;
#include<string>
//1.创建学生的数据类型
struct student{
//名字
string name;
//年龄
int age;
//成绩
int score;
};
void printf1(struct student stu)//struct可以省略
{
stu.age = 100;
cout << " 姓名:" << stu.name << " 年龄:" << stu.age << " 分数:" << stu.score << endl;
}
void printf2(struct student *stu)
{
stu->age = 200;
cout << " 姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
}
int main()
{
struct student s = { "张三", 18, 100 };
//在函数中打印
//1.值传递
cout << "在函数1中打印" << endl;
printf1(s);
cout << "在main中打印" << endl;
cout << " 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
cout << "在函数2中打印" << endl;
printf2(&s);
cout << "在main中打印" << endl;
cout << " 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
system("pause");
return 0;
}
可知值传递不会改变main函数中的数据,而地址传递会改变main函数中的数据
1.7结构体中const使用场景
作用:防止误操作
将输出函数中的参数改为指针类型,可以减少内存空间(无论里面结构体包含多少成员,指针只占用4个字节) 而且不会有新的副本出来。而为了防止在输出函数中误将信息修改,就可以在参数前面加上const eg:(const student stu),这时一旦误操作修改了信息,系统就会报错