700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针

c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针

时间:2022-09-20 20:15:23

相关推荐

c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针

十二、结构体

(1)结构体定义和使用

基本概念:结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。

参考视频:/video/BV1et411b73Z?from=search&seid=4205594350351753444

已投币三连,非常好的视频教程,感谢up主的奉献。

结构体定义和使用:

语法:

struct 结构体名 { 结构体成员列表 };

通过结构体创建变量的方式有三种:

struct 结构体名 变量名;struct 结构体名 变量名 = { 成员1值, 成员2值... };定义结构体时顺便创建变量;

代码:

/* ------------------ 1.struct 结构体名 变量名; 之后进行赋值操作 ------------------ */struct Student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};int main() {//struct Student xiaoMing;Student xiaoMing;// struct关键字可以省略xiaoMing.name = "小明";xiaoMing.age = 18;xiaoMing.score = 96;xiaoMing = { "小明", 18, 96 };cout << "姓名:" << xiaoMing.name << " 年龄:" << xiaoMing.age << " 分数:" << xiaoMing.score << endl;system("pause");return 0;}/* -------------- 2.struct 结构体名 变量名 = { 成员1值, 成员2值... }; -------------- */struct Student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};int main() {Student xiaoMing = {"小明", 18, 96};cout << "姓名:" << xiaoMing.name << " 年龄:" << xiaoMing.age << " 分数:" << xiaoMing.score << endl;system("pause");return 0;}/* -------------------- 3.定义结构体时顺便创建变量; -------------------- */struct Student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};int main() {xiaoMing.name = "小明";xiaoMing.age = 18;xiaoMing.score = 96;xiaoMing = { "小明", 18, 96 };cout << "姓名:" << xiaoMing.name << " 年龄:" << xiaoMing.age << " 分数:" << xiaoMing.score << endl;system("pause");return 0;}-----------------------------------------------------------------------------------姓名:小明 年龄:18 分数:96请按任意键继续. . .

使用 typedef 定义 struct 结构体方法:/Mark_md/article/details/107656790

(2)结构体数组

作用:将自定义的结构体放入到数组中,方便维护。

语法:

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

struct Student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};int main() {struct Student stuArray[3] ={{ "小明", 18, 96 },{ "小红", 20, 100 },{ "小亮", 21, 98 }};for (int i = 0; i < 3; i++){cout << "姓名:" << stuArray[i].name << " 年龄:" << stuArray[i].age << " 分数:" << stuArray[i].score << endl;}system("pause");return 0;}----------------------------------------------------------------------------姓名:小明 年龄:18 分数:96姓名:小红 年龄:20 分数:100姓名:小亮 年龄:21 分数:98请按任意键继续. . .

(3)结构体指针

作用:通过指针访问结构体中的成员。

利用操作符 -> 可以通过结构体指针访问结构体属性。

示例:

struct Student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};int main() {struct Student xiaoMing = { "小明", 18, 96 };Student* p = &xiaoMing;cout << "姓名:" << p->name<< " 年龄:" << p->age<< " 分数:" << p->score << endl;system("pause");return 0;}-----------------------------------------------------------------姓名:小明 年龄:18 分数:96请按任意键继续. . .

(4)结构体嵌套

结构体嵌套结构体:

作用:结构体中的成员可以是另一个结构体。

例如:每个老师辅导一个学员,老师的结构体中,记录学生的结构体。

示例:

struct student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};struct teacher{int id;// 职工编号string name;// 教师姓名int age;// 教师年龄struct student stu;// 子结构体 学生};int main() {teacher wang;wang.id = 112374;wang.name = "王芳";wang.age = 26;wang.stu.name = "小明";wang.stu.age = 18;wang.stu.score = 96;cout << "教师姓名:" << wang.name<< " 教师年龄:" << wang.age<< " 教师ID号:" << wang.id << endl;cout << "学生姓名:" << wang.stu.name<< " 学生年龄:" << wang.stu.age<< " 学生分数:" << wang.stu.score << endl;system("pause");return 0;}-------------------------------------------------------------------------教师姓名:王芳 教师年龄:26 教师ID号:112374学生姓名:小明 学生年龄:18 学生分数:96请按任意键继续. . .

(5)结构体做函数参数

作用:将结构体作为参数向函数中传递

传递方式分两种:值传递,地址传递。

值传递示例:

struct student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};void printStudent(student stu){cout << "姓名:" << stu.name<< " 年龄:" << stu.age<< " 分数:" << stu.score << endl;stu.name = "无";stu.age = 0;stu.score = 0;}int main() {student xiaoMing = { "小明", 18, 96 };printStudent(xiaoMing);cout << "姓名:" << xiaoMing.name<< " 年龄:" << xiaoMing.age<< " 分数:" << xiaoMing.score << endl;system("pause");return 0;}---------------------------------------------------------------------------姓名:小明 年龄:18 分数:96姓名:小明 年龄:18 分数:96请按任意键继续. . .

地址传递示例:

struct student{// 成员列表string name; // 姓名int age;// 年龄int score;// 分数};void printStudent(student * stu){cout << "姓名:" << stu->name<< " 年龄:" << stu->age<< " 分数:" << stu->score << endl;stu->name = "无";stu->age = 0;stu->score = 0;}int main() {student xiaoMing = { "小明", 18, 96 };printStudent(&xiaoMing);cout << "姓名:" << xiaoMing.name<< " 年龄:" << xiaoMing.age<< " 分数:" << xiaoMing.score << endl;system("pause");return 0;}--------------------------------------------------------------------------姓名:小明 年龄:18 分数:96姓名:无 年龄:0 分数:0请按任意键继续. . .

如果不想实参结构体内容,用值传递,反之用地址传递。

地址传递还可以使用数组,可实现同样功能,书写与阅读比指针更加直观。

(6)结构体中const使用场景

作用:用const来防止误操作

值传递会copy,会占内存空间,复制也会多花时间。所以如果可以用地址传递,可以提高执行效率。

而地址传递可能会对原始对象进行误修改,这时候就可以加 const。如果出现修改,编译器会报错

示例:

void printStudent(const student * stu){cout << "姓名:" << stu->name<< " 年龄:" << stu->age<< " 分数:" << stu->score << endl;stu->name = "无"; // 编译器会报错stu->age = 0;stu->score = 0;}

c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针 结构体嵌套 结构体做函数参数 结构体const

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。