> 文章列表 > C++基础入门丨8. 结构体——还需要知道这些

C++基础入门丨8. 结构体——还需要知道这些

C++基础入门丨8. 结构体——还需要知道这些

Author:AXYZdong 硕士在读 工科男
有一点思考,有一点想法,有一点理性!
定个小小目标,努力成为习惯!在最美的年华遇见更好的自己!
CSDN@AXYZdong,CSDN首发,AXYZdong原创
唯一博客更新的地址为: 👉 AXYZdong的博客 👈
B站主页为:AXYZdong的个人主页

系列文章目录

  • C++基础入门丨1. 初识C++像极了C语言
  • C++基础入门丨2. 数据类型基础
  • C++基础入门丨3. 搞明白4类运算符——运算符
  • C++基础入门丨4. 程序结构有哪几种?——程序流程结构
  • C++基础入门丨5. 数组——一维数组和二维数组
  • C++基础入门丨6. 函数——定义、调用和声明
  • C++基础入门丨7. 指针——一文搞懂指针

操作系统:Windows 10
IDE:Visual Studio 2019

文章目录

      • 系列文章目录
    • 1 什么是结构体
    • 2 结构体的定义和使用
    • 3 结构体数组
    • 4 结构体指针
    • 5 结构体嵌套结构体
    • 6 结构体做函数参数
    • 7 结构体中 const 使用场景

1 什么是结构体

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

2 结构体的定义和使用

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

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

  1. struct 结构体名 变量名
  2. struct 结构体名 变量名 = { 成员1值 , 成员2值…}
  3. 定义结构体时顺便创建变量
#include<iostream>
#include<string>
//学生结构体定义
struct Student
{string s_name;int score;
};
//老师结构体定义
struct Teacher
{string t_name;Student sArray[5];
};int main()
{Teacher tArray[3];		//创建三名老师的数组system("pause");return 0;	
}
  • 定义结构体时的关键字是 struct ,不可省略
  • 创建结构体变量时,关键字 struct 可以省略
  • 结构体变量利用操作符 . 访问成员

3 结构体数组

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

语法:struct 结构体名 数组名[元素个数] = { {} , {} , … {} }

	//结构体数组struct student arr[3]={{"张三",18,80 },{"李四",19,60 },{"王五",20,70 }};

4 结构体指针

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

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

#include<iostream>
#include<string>using namespace std;//结构体定义
struct student
{//成员列表string name;  //姓名int age;      //年龄int score;    //分数
};int main() {struct student stu = { "张三",18,100, };struct student* p = &stu;p->score = 80; //指针通过 -> 操作符可以访问成员cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;system("pause");return 0;
}

C++基础入门丨8. 结构体——还需要知道这些

5 结构体嵌套结构体

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

//学生结构体定义
struct Student
{string s_name;int score;
};
//老师结构体定义
struct Teacher
{string t_name;Student sArray[5];	//嵌套了学生的结构体
};
  • 在结构体中可以定义另一个结构体作为成员,用来解决实际问题

6 结构体做函数参数

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

传递方式有两种:

  • 值传递
  • 地址传递
#include<iostream>
#include<string>using namespace std;//结构体定义
struct student
{//成员列表string name;  //姓名int age;      //年龄int score;    //分数
};
//值传递
void printstudent1(student stu)
{stu.age = 20;cout << "姓名:" << stu.name << " 年龄:" << stu.age << " 分数:" << stu.score << endl;
}
//地址传递
void printstudent2(student * stu)
{stu->age = 20;cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
}
int main() {struct student stu = { "张三",18,100, };printstudent1(stu);cout << "主函数中 姓名:" << stu.name << " 年龄:" << stu.age << " 分数:" << stu.score << endl;printstudent2(&stu);cout << "主函数中 姓名:" << stu.name << " 年龄:" << stu.age << " 分数:" << stu.score << endl;system("pause");return 0;
}

C++基础入门丨8. 结构体——还需要知道这些

  • 在使用地址传递时,形参的改变会影响实参,在上述程序中也就是改变了主函数中的数据。而值传递不会改变主函数中的数据。
  • 如果不想修改主函数中的数据,用值传递,反之用地址传递。

7 结构体中 const 使用场景

const 来防止误操作。

void printStudent(const student *stu) //加const防止函数体中的误操作
{//stu->age = 100; //操作失败,因为加了const修饰cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;}
  • 在结构体指针前加 const 时,就无法通过结构体指针来修改数据,也就是写操作失效,只可进行读操作。

Reference

  • [1]:https://www.bilibili.com/video/BV1VJ411M7WR

—— END ——


如果以上内容有任何错误或者不准确的地方,欢迎在下面 👇 留言。或者你有更好的想法,欢迎一起交流学习~~~

更多精彩内容请前往 AXYZdong的博客