结构体

结 构 体 结构体

63.结构体-结构体定义和使用

1 结构体基本概念

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

2 结构体定义和使用

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

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

  • struct结构体名 变量名
  • struct结构体名 变量名={成员1值,成员2值…}
  • 定义结构体时顺便创建变量
#include <iostream>
using namespace std;

// 1. 创建学生数据类型
struct Student {
// 成员列表
    // 姓名
    string name;
    // 年龄
    int age;
    // 分数
    int score;

}s3;  // 2.3 定义结构体时,直接创建结构变量

//2.通过学生类型创建具体的学生
int main()
{
    //2.1 struct Student s1
    struct Student s1;
    s1.name = "major";
    s1.age = 22;
    s1.score = 100;
    cout << "name:  " << s1.name << "  age:  " << s1.age << "  score:  " << s1.score << endl;


    // 2.2 struct Student s2 ={...}
    struct Student s2 = { "major_S",23,110 };
    cout << "name:  " << s2.name << "  age:  " << s2.age << "  score:  " << s2.score << endl;

    // 2.3 定义结构体时,直接创建结构变量
    s3.name = "majoe_shen";
    s3.age = 25;
    s3.score = 160;
    cout << "name:  " << s3.name << "  age:  " << s3.age << "  score:  " << s3.score << endl;

    // struct关键字可以省略
    Student s4 = { "major_",30,999999 };
    cout << "name:  " << s4.name << "  age:  " << s4.age << "  score:  " << s4.score << endl;
}


在这里插入图片描述

64.结构体-结构体数组

结构体数组:
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {},{}, ...,{}}

#include <iostream>
using namespace std;

// 结构体数组

// 1.定义结构体
struct Student {
	// 姓名
	string name;
	// 年龄
	int age;
	// 分数
	int score;
};

int main()
{
// 2.创建结构体数组
	struct Student subArray[3] = {
		{"major1",18,100},
		{"major2",28,999999},
		{"major3",38,999999999999999},
	};
// 3.给结构体数组中的元素赋值
	subArray[2].name = "smj";

// 4.遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "name : " << subArray[i].name<<endl;
	}
}


在这里插入图片描述

65.结构体-结构体指针

结构体指针
作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性

#include <iostream>
using namespace std;

// 结构体指针

// 定义学生结构体
struct student {
	string name;
	int age;
	int score;
};

int main()
{
	// 1.创建学生结构体变量
	student s = { "major",22,100 };
	// 2.通过指针指向结构体变量
	student * p = &s;
	// 3.通过指针访问结构体变量中的数据
	cout << "name:  " << p->name << endl;

}


在这里插入图片描述

66.结构体-结构体嵌套结构体

结构体嵌套结构体 。
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

#include <iostream>
using namespace std;

// 结构体嵌套结构体

// 定义学生结构体
struct student {
	string name;
	int age;
	int score;
};

struct teacher {
	int id;
	string name;
	int age;
	struct student stu; // 带的学生
};

int main()
{

	// 创建老师
	teacher t;
	t.id = 10000;
	t.name = "major";
	t.age = 22;
	t.stu.name = "major_stu1";
	t.stu.age = 18;
	t.stu.score = 100;

	cout << "teacher's name: " << t.name << endl;
	cout << "teacher's stu'name: " << t.stu.name << endl;

}


在这里插入图片描述

67.结构体-结构体做函数参数

结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:

  • 值传递
  • 地址传递
#include <iostream>
using namespace std;

// 结构体做函数参数

// 定义学生结构体
struct student {
	string name;
	int age;
	int score;
};

// 打印学生信息
// 1.值传递
void printStu(struct student s) {
	s.name = "major_S";
	cout << "值传递:: " << s.name << endl;
}

// 2.地址传递
void printStu2(struct student * p) {
	p->name = "major_SSSS";
	cout << "地址传递:: " << p->name << endl;
}


int main()
{
	// 将学生传入到一个参数中,打印学生身上的所有信息
	struct student s;
	s.name = "major";
	s.age = 20;
	s.score = 100;
	cout << "main :: name: " << s.name << endl;
	printStu(s);
	printStu2(&s);
	cout << "main :: name: " << s.name << endl;
}


在这里插入图片描述

总结:如果不想修改主函数中的数据,用值传递,反之,用地址传递

68.结构体-结构体中const使用场景

作用:用const来防止误操作

#include <iostream>
using namespace std;

// 结构体做函数参数

// 定义学生结构体
struct student {
	string name;
	int age;
	int score;
};

void printStu(const student *s ) {

	//s->age = 100; 错误
	cout << "name :  " << s->name << endl;
}


int main()
{
	student s = { "major",25,69999 };
	printStu(&s);

}


在这里插入图片描述

69.结构体-结构体案例1

70.结构体-结构体案例2

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值