C++基础(7)——结构体

本文详细介绍了C++中的结构体,包括结构体的概念、定义和使用方法,如通过结构体创建变量、结构体数组、结构体指针、结构体嵌套结构体以及结构体作为函数参数的使用。此外,还探讨了结构体在函数中的传递方式和const的使用,强调了值传递和地址传递的区别以及const在防止误操作中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.结构体概述

1.1结构体的概念

1.2结构体的定义和使用

2.结构体数组

 3.结构体指针

4.结构体嵌套结构体 

5.结构体做函数参数


1.结构体概述

1.1结构体的概念

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

1.2结构体的定义和使用

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

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

       (1)struct 结构体名 变量名

       (2)struct  结构体名 变量名 ={成员1值,成员2值……}

       (3)定义结构体时顺便创建变量       

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;

//1.创建学生数据类型
struct Student
{
	//成员列表
	string name;
	int age;
	int score;
}s3;

int main() {
	//2.通过学生类型创建具体学生,有三种方式
/*
	(1)struct Student s1
	(2)struct Student s2={......}
	(3)在定义结构体时顺便创建结构体变量
*/
	//(1)种:struct Student s1
	struct Student s1;
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;

	cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << endl;

	//(2)种:struct Student s2={......}
	struct Student s2 = { "李四",19,80 };
	cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << endl;

	//(3)在定义结构体时顺便创建结构体变量,在前面定义结构体的时候在末尾定义结构变量
	s3.name = "王五";
	s3.age = 20;
	s3.score = 60;
	cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << endl;


}

2.结构体数组

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

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

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;

//结构体数组
//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

int main() {
	//2.创建结构体数组
	struct Student stuArray[] = {
		{"alice",18,100},
		{"Bob",20,99},
		{"helon",21,70}
	};
	//3.给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	//4.遍历结构体数组
	for (int i = 0; i < 3; i++) {
		cout << "neme:" << stuArray[i].name << "age:"
			<< stuArray[i].age << "score:" 
			<< stuArray[i].score << endl;
	}

}

 

 3.结构体指针

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

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

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;

//结构体数组
//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

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

}

4.结构体嵌套结构体 

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;

//结构体套结构体
//1.定义结构体
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 = "Bob";
	t.age = 20;
	t.stu.name = "HELON";
	t.stu.age = 20;
	t.stu.score = 60;
	cout << t.name << t.age << t.id << t.stu.name<< endl;

}

5.结构体做函数参数

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

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;

//结构体套结构体
//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

//打印学生信息函数
//way1.值传递
void printStudent1(struct Student s) {
	cout << "子函数1中 姓名:" << s.name << "     年龄:  " << s.age 
		<< "    分数:    " << s.score << endl;
}

//way2.地址传递
void printStudent2(struct Student* p) {
	cout << "子函数2中 姓名:" << p->name << "     年龄:  " << p->age
		<< "    分数:    " << p->score << endl;
}

int main() {
	//将学生传入到一个参数中,打印学生的全部信息
	struct Student s;
	s.name = "alice";
	s.age = 20;
	s.score = 88;
	printStudent1(s);
	printStudent2(&s);
}

6.结构体中的const使用

作用:用const来防止误操作

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;


//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

void printStudent(Student s) {
	cout << "name:     " << s.name << "     age:     " << s.age << "   score:   " << s.score << endl;
}

int main() {
	struct Student s = { "Alice",15,79 };
	//通过函数打印结构体信息
	printStudent(s);
}

上面是值传递,printStudent里面的数据与main函数的数据不是一个数据。因为printStudent里面的数据改变,不会影响main函数中的数据。假设一个学校有成千上万的人需要查成绩,那就需要把这个数据复制成千上万次,占用内存就非常多,因此我们可以使用地址传递,将形参改为指针,指针指占用4个字节,而且不会复制新的副本出现,如下所示:

#include<iostream>
#include"swap.h"
#include<string>
using namespace std;


//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

void printStudent(Student *s) {
	cout << "name:     " << s->name << "     age:     " << s->age << "   score:   " << s->score << endl;
}

int main() {
	struct Student s = { "Alice",15,79 };
	//通过函数打印结构体信息
	printStudent(&s);
}

但是地址传递存在一定的安全隐患,例如如果我们不小心把张三的年龄改为150岁,我本来只是想打印张三的信息,但是误操作改了年龄,这时候main函数中的张三的年龄都改了。

为了防止误操作出现,我们可以在形参中加一个const,如下所示:

 加一个const以后,不能对结构体s使用任何操作,只有对他进行写就会报错,只能进行读操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI炮灰

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值