C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
目录
C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
1、结构体创建
2、结构体创建代码演示
3、结构体数组
4、结构体数组代码演示
5、结构体指针
6、结构体指针代码
7、结构体嵌套结构体
8、结构体嵌套结构体代码
9、结构体做函数参数
10、结构体做函数参数代码演示
11、结构体const变量使用场景
1、结构体创建
- struct结构体名 变量名
- struct 结构体名 变量名={成员值1,成员值2,...}
- 定义结构体时顺便定义变量
2、结构体创建代码演示
#include<iostream>
#include<string>
using namespace std;
//通过结构体创建变量的方式有三种
//1、struct结构体名 变量名
//2、struct 结构体名 变量名={成员值1,成员值2,...}
//3、定义结构体时顺便定义变量
struct Student
{string name;int age;int score;
};
struct Student2
{string name;int age;int score;
}s3;
int main() {//第一种Student s1;s1.name = "张三";s1.age = 18;s1.score = 99;cout << "name=" << s1.name << " age=" << s1.age << " score=" << s1.score << endl;//第二种Student s2 = {"王二",18,97};cout << "name=" << s2.name << " age=" << s2.age << " score=" << s2.score << endl;//第三种s3.name = "王二";s3.age = 17;s3.score = 78;cout << "name=" << s3.name << " age=" << s3.age << " score=" << s3.score << endl;system("pause");return 0;
}
name=张三 age=18 score=99
name=王二 age=18 score=97
name=王二 age=17 score=78
请按任意键继续. . .
3、结构体数组
- 定义结构体
//1、定义结构体
struct student
{string name;int age;int score;
};
- 创建结构体数组
//2、创建结构体数组struct student stuarr[3] = {{"张三",18,100},{"李四",17,90},{"王二",16,80}};
- 给结构体数组中的元素赋值
//3、给结构体数组中的元素赋值stuarr[2].name = "李三";stuarr[2].age = 17;stuarr[2].score = 10;
- 遍历结构体数组
for (int i = 0; i < 3; i++){cout << "姓名:" << stuarr[i].name << "\t年龄:" << stuarr[i].age << "\t分数:" << stuarr[i].score << endl;}
4、结构体数组代码演示
#include<iostream>
#include<string>
using namespace std;
//结构体数组
//1、定义结构体
struct student
{string name;int age;int score;
};int main() {//2、创建结构体数组struct student stuarr[3] = {{"张三",18,100},{"李四",17,90},{"王二",16,80}};//3、给结构体数组中的元素赋值stuarr[2].name = "李三";stuarr[2].age = 17;stuarr[2].score = 10;//4、遍历结构体数组for (int i = 0; i < 3; i++){cout << "姓名:" << stuarr[i].name << "\t年龄:" << stuarr[i].age << "\t分数:" << stuarr[i].score << endl;}system("pause");return 0;
}
姓名:张三 年龄:18 分数:100
姓名:李四 年龄:17 分数:90
姓名:李三 年龄:17 分数:10
请按任意键继续. . .
5、结构体指针
- 创建学生结构体变量
- 通过指针指向结构体变量
- 通过指针访问结构体变量中的数据,过结构体指针访问结构体中的属性,需要利用“->”
6、结构体指针代码
#include<iostream>
#include<string>
using namespace std;
//结构体数组
struct student
{string name;int age;int score;
};int main() {//1、创建学生结构体变量student s1 = { "李四",19,90 };//2、通过指针指向结构体变量student *p = &s1;//3、通过指针访问结构体变量中的数据cout << "姓名:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score<<endl;//通过结构体指针 访问结构体中的属性,需要利用“->”system("pause");return 0;
}
姓名:李四 年龄:19 分数:90
请按任意键继续. . .
7、结构体嵌套结构体
在一个结构体中包含另一个结构体变量,需要事先定义好该结构体。
struct student
{string name;int age;int score;
};
struct teacher
{string name;int age;string id;struct student stu;
};
8、结构体嵌套结构体代码
#include<iostream>
#include<string>
using namespace std;
//结构体嵌套结构体
struct student
{string name;int age;int score;
};
struct teacher
{string name;int age;string id;struct student stu;
};
int main() {teacher t1;t1.id = "001";t1.name = "大王";t1.age = 50;t1.stu.name = "小王";t1.stu.age = 15;t1.stu.score = 90;cout << t1.id << endl << t1.name << endl << t1.age << endl<< t1.stu.name << endl << t1.stu.age << endl << t1.stu.score << endl;system("pause");return 0;
}
001
大王
50
小王
15
90
请按任意键继续. . .
9、结构体做函数参数
作用:将结构体作为参数向函数传递
方式:值传递、地址传递
10、结构体做函数参数代码演示
#include<iostream>
#include<string>
using namespace std;
//结构体做函数参数
//作用:将结构体作为参数向函数传递
//传递的方式有两种:值传递、地址传递
struct student
{string name;int age;int score;
};
//值传递
void print(student s1) {cout << "值传递" << endl;s1.age = 100;cout << "子函数姓名:" << s1.name << "\t年龄:" << s1.age << "\t分数:" << s1.score << endl;
}
//地址传递
void print2(student *s) {cout <<"地址传递" << endl;s->age = 90;cout << "子函数姓名:" <<s->name<< "\t年龄:" << s->age << "\t分数:" << s->score << endl;
}
int main() {student s1;s1.name = "Lijian";s1.age = 25;s1.score = 100;print(s1);cout << "主函数姓名:" << s1.name << "\t年龄:" << s1.age << "\t分数:" << s1.score << endl;print2(&s1);cout << "主函数姓名:" << s1.name << "\t年龄:" << s1.age << "\t分数:" << s1.score << endl;system("pause");return 0;
}
值传递
子函数姓名:Lijian 年龄:100 分数:100
主函数姓名:Lijian 年龄:25 分数:100
地址传递
子函数姓名:Lijian 年龄:90 分数:100
主函数姓名:Lijian 年龄:90 分数:100
请按任意键继续. . .
11、结构体const变量使用场景
加入const之后,一旦有修改操作就会报错,可以防止我们的误操作对外面数据进行修改。
void print3(const student *s) {//加入const之后,一旦有修改操作就会报错,可以防止我们的误操作对外面数据进行修改cout << "const变量使用场景" << endl;//s->age = 90;错误cout << "子函数姓名:" << s->name << "\t年龄:" << s->age << "\t分数:" << s->score << endl;
}