c/c++教程 - 1.10 结构体 使用typedef定义struct结构体 结构体数组 结构体指针 结构体嵌套 结构体做函数参数 结构体const
十二、结构体
(1)结构体定义和使用
基本概念:结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。
参考视频:https://www.bilibili.com/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 结构体方法:https://blog.csdn.net/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相关推荐
- C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例
C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例 目录 一.简单介绍 二.结构体定义和使用 三. ...
- c++结构体总结(结构体定义,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,结构体中 const使用场景)
看完b站黑马程序员之后的借鉴和笔记 1.什么是结构体,有什么作用? 在C/C++中,结构体是用户定义的数据类型.它可以把几种不同类型的数据项集合成结构体这样一个单一类型. 2. 结构体定义和使用 #i ...
- C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用) 目录 C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数, ...
- 结构体05:结构体做函数参数
#include<iostream> #include<string> using namespace std;//定义结构体 struct Student{string na ...
- 【C 语言】结构体 ( 结构体作为函数参数 | 结构体指针作为函数参数 )
文章目录 一.结构体作为函数参数 二.结构体指针作为函数参数 三.完整代码示例 一.结构体作为函数参数 结构体变量 作为函数形参 , 在函数中 , 只能访问 该函数形参 , 无法修改 结构体内存 的值 ...
- 【C++】结构体 - 定义和使用,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,结构体 const
文章目录 1. 定义和使用 2. 结构体数组 3. 结构体指针 4. 结构体嵌套结构体 5. 结构体做函数参数 6. 结构体 const 1. 定义和使用 结构体属于用户自定义的数据类型,允许用户存储 ...
- C++结构体 结构体定义和使用、结构体数组、结构体指针、结构体嵌套结构体、结构体做函数参数
C++结构体 第二章 C++结构体 1.结构体定义和使用 语法:struct 结构体名 { 结构体成员列表 }: 通过结构体创建变量的方式有三种: struct 结构体名 变量名 struct 结构体 ...
- c/c++教程 - 2.2 引用的使用方法,引用做函数参数,引用做返回值,引用的本质,常量引用
2. 引用 学习目的:取代C中的指针. 目录 2. 引用 2.1 引用的基本使用方法 2.2 引用注意事项 2.3 引用做函数参数 2.4引用做函数返回值 2.5 引用的本质 2.6 常量引用 相关教 ...
- c语言 结构体指针做函数参数
demo:修改某个学生的成绩 不利用指针直接在主函数内修改学生成绩的代码: #include <stdio.h> #include <string.h> #define N 5 ...
最新文章
- DQL查询语句约束数据库设计备份和还原
- angular监听图片加载完成_angular1.0 如何监听页面渲染完毕 (转)
- __call处理调用错误
- 差异备份、增量备份、完全备份的区别
- html5 判断手机电脑,H5_0006:JS判断PC,平板,手机平台的方法
- Java伪界面操作数据库的小实例
- 通过OWA修改密码,提示您输入的密码不符合最低安全要求
- 制作唐诗网页代码_分享FrontPage制作网页的几个技巧
- java日期的运用(DateUtils工具类)
- matlab 信号分选 聚类_显示微缩化,对测试分选设备提出了怎样的要求?
- ckeditor 触发事件(案例)
- java中的T extends Comparable ? super T
- python去年软件排行_2016 年有哪些好的 Python 机器学习开源项目?
- WIN10系统 Indirect Display 虚拟显示器之特殊应用
- 图形验证码实现(代码)
- Node.js入门(含NVM、NPM、NVM的安装)-(转载)
- 中国区块链专利申请数破万:阿里巴巴居首位 网心科技晋身前十
- android vendor接口,HIDL众多service在那启动的--/vendor/etc/init/android.hardware.*@2.0-service.rc...
- 女性内分泌失调要小心
- 5G网速真的有理论上那么高吗?