C++学习之旅 - 结构体

文章详细介绍了C++中的结构体,包括如何定义结构体、使用结构体变量、结构体数组、结构体指针、嵌套结构体以及结构体作为函数参数的值传递和地址传递。此外,还讨论了在结构体中使用const关键字来防止意外修改成员的场景。
摘要由CSDN通过智能技术生成

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

结构体定义和使用

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

定义
//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct Student {
  //成员列表
	string name;
	int age;
	int score;
};
使用
  1. struct Student s1;
  2. struct Student s2 = {....};
#include <iostream>

using namespace std;

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

int main(void) {
	struct Student s1;
	//通过结构体变量.变量中的属性
	s1.name = "李四";
	s1.age = 18;
	s1.score = 90;
	cout << "学生1姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;

	struct Student s2 = { "王五",19,95 };
	cout << "学生2姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;
}

结构体数组

学生量比较大的时候就可以考虑放入一个数组中

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

#include <iostream>

using namespace std;

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

int main(void) {
	struct Student stuArray[3] = { 
		{"张三",18,80},
		{"李四",19,88},
		{"王五",19,79}
	};

	//修改元素
	stuArray[2].name = "赵四";
	stuArray[2].age = 20;
	stuArray[2].score = 60;
	for (int i = 0; i < 3; i++) {
		cout << "学生1姓名:" << stuArray[i].name << "年龄:" << stuArray[i].age << "分数:" << stuArray[i].score << endl;
	}
}

结构体指针

#include <iostream>

using namespace std;

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

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

结构体嵌套结构体

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
  //成员列表
	string name;
	int age;
	int score;
};
struct teacher {
	int id;
	string name;
	student stu;
};

int main(void) {
	student s1 = { "张三",18,65 };
	teacher t1 = { 0,"王老师",s1 };
	cout << "老师的名称:" << t1.name << "带的学生名字:" << t1.stu.name << endl;
}

结构体做函数参数

值传递
#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
  //成员列表
	string name;
	int age;
	int score;
};

void printStudent(student stu) {
	cout << stu.name << endl;
}

int main(void) {
	student s1 = {"张三",16,91};
	printStudent(s1);
}
地址传递
#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
  //成员列表
	string name;
	int age;
	int score;
};

//将形参改为指针,可以减少内存空间
void printStudent(student *stu) {
	cout << stu.name << endl;
}
void setStudentName(student* stu,string name) {
	stu->name = name;
}

int main(void) {
	student s1 = {"张三",16,91};
	printStudent(&s1);
	setStudentName(&s1,"李四");
	printStudent(&s1);
}

在这里插入图片描述

结构体中const使用场景

这个防止我们误操作
其实就是防止我们有写的操作

#include <iostream>

using namespace std;

//1. 创建学生数据类型: 学生包括(姓名,年龄,分数)
struct student {
  //成员列表
	string name;
	int age;
	int score;
};

void printStudent(student stu) {
	cout << stu.name << endl;
}
void setStudentName(student* stu,string name) {
	stu->name = name;
}

int main(void) {
	const student s1 = {"张三",16,91}; //加上了const
	printStudent(s1);
	setStudentName(&s1,"李四");
	printStudent(s1);
}

在这里插入图片描述

一旦有改变元素的行为就会报错

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结城明日奈是我老婆

支持一下一直热爱程序的菜鸟吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值