结构体

初识结构体:

什么是结构体:结构体是由一些数据类型的集合组成的类型

结构体的作用:为了能够自定义数据类型

结构体的格式:struct 结构体名 {结构体成员列表};

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

1. struct 结构体名 变量名;

2. struct 结构体名 变量名 = {成员变量1的值,成员变量2的值} ;

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

#include <iostream>
#include <string>
using namespace std;

struct student {
	string  name;
	int age;
	int score;
};

//第三种方式,定义结构体的同时声明结构体变量
struct teacher {
	string  name;
	int age;
} te;

int main()
{
	struct  student st1; //第一种方式 
	st1.name = "xiaowang";
	st1.age = 21;
	st1.score = 80;
	struct  student st2 = { "xiaoli", 20, 18 };//第二种方式
	te.name = "Mr.Zhao";
	system("pause");
	return 0;
}

结构体数组:

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

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

示例:

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	//创建结构体数组
	struct student arr[2] =
	{
		{"张三", 21, 89},
		{"李四", 20, 90}
	};
	//变更数组中的值
	arr[1].age = 30;
	//遍历结构体数组
	for (int i = 0; i < 2; i++)
	{
		cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
	}

	system("pause");
	return 0;
}

结构体指针:

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

语法:struct 结构体名 * 指针名;

示例:

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	//创建结构体变量
	struct student stu = { "张三", 21, 89 };
	//创建结构体指针
	struct student *p = &stu;
	//访问结构体指针中的元素
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

	system("pause");
	return 0;
}

结构体中嵌套结构体:

示例:

#include <iostream>
#include <string>
using namespace std;

//定义一个学生结构体
struct student
{
	string name;
	int age;
	int score;
};
//定义一个老师结构体,老师中包含一个学生结构体
struct  teacher
{
	int id;
	string name;
	struct  student stu;
};

int main()
{
	//定义结构体变量
	struct teacher tea;
	//给结构体变量赋值
	tea.id = 1;
	tea.name = "王老师";
	tea.stu.name = "小李";
	tea.stu.age = 19;
	tea.stu.score = 90;
	//输出结构体变量的值
	cout << "老师姓名:" << tea.name
		<< " 老师编号:" << tea.id
		<< " 老师辅导的学生姓名:" << tea.stu.name
		<< " 老师辅导的学生年龄:" << tea.stu.age
		<< " 老师辅导的学生成绩:" << tea.stu.score << endl;
	system("pause");
	return 0;
}

结构体用作函数参数

示例:

#include <iostream>
#include <string>
using namespace std;


//定义一个学生结构体
struct student
{
	string name;
	int age;
	int score;
};
//定义一个老师结构体,老师中包含一个学生结构体
struct  teacher
{
	int id;
	string name;
	struct  student stu;
};

//值传递函数,用于输出结构体变量的值
void print(teacher tea)
{
	cout << "老师姓名:" << tea.name << " 老师编号:" << tea.id << " 学生姓名:" << tea.stu.name << " 学生年龄:" << tea.stu.age << " 学生成绩:" << tea.stu.score << endl;
}

//引用传递函数,用于输出和修改结构体变量的值
void print1(teacher *tea)
{
	cout << "老师姓名:" << tea->name << " 老师编号:" << tea->id << " 学生姓名:" << tea->stu.name << " 学生年龄:" << tea->stu.age << " 学生成绩:" << tea->stu.score << endl;
	tea->name = "赵老师";
}

int main()
{
	//定义结构体变量
	struct teacher tea;
	//给结构体变量赋值
	tea.id = 1;
	tea.name = "王老师";
	tea.stu.name = "小李";
	tea.stu.age = 19;
	tea.stu.score = 90;
	//print(tea);
	print1(&tea);
	cout << "修改后老师的姓名:" << tea.name  << endl;
	system("pause");
	return 0;
}

结构体中const的使用

格式示例:const  struct teacher tea = {1,"王老师"};  //const修饰了结构体变量后,变量的值不能再被修改

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值