结构体

结构体

概述
结构体的定义与使用
结构体数组
结构体指针
结构体做函数参数
结构体中const的使用场景

概述

处理问题比较复杂,需要将不同类型的数据结合到一起,以供用户更加方便使用。这个组合在一起的数据是相互联系的。例如一个学生的姓名、年龄、分数
int 、double 、char

结构体的定义与使用

#include<iostream>
using namespace std;
//如何定义结构体
//语法:struct 结构体名{成员列表};
//结构体类型是sturct+结构体名,struct student
struct student {
	//成员列表
	//1.姓名
	string name;
	//2.年龄
	int age;
	//3.分数
	int score;
};
struct student {
	//成员列表
	//1.姓名
	string name;
	//2.年龄
	int age;
	//3.分数
	int score;
}stu2;
struct {
	//成员列表
	//1.姓名
	string name;
	//2.年龄
	int age;
	//3.分数
	int score;
}stu3;

int main() {
	//创建结构体变量
	//1.先定义结构体类型,再定义结构体变量
	struct student stu;
	//给结构体赋值的方法:结构体变量名,成员变量名
	stu.name = "yoiliangyu";
	stu.age = 24;
	stu.score = 25;
	cout << "姓名:" << stu.name<<"\t";
	cout << "年龄:" << stu.age<<"\t";
	cout << "分数:" << stu.score<<endl;
	//2.直接在定义的同时直接初始化
	struct student stu1 = { "shabi",25,100 };
	cout << "姓名:" << stu1.name << "\t";
	cout << "年龄:" << stu1.age << "\t";
	cout << "分数:" << stu1.score << endl;
	//3.定义结构体的同时,定义结构体变量
	stu2.name = "wuyu";
	stu2.age = 24;
	stu2.score = 25;
	cout << "姓名:" << stu2.name << "\t";
	cout << "年龄:" << stu2.age << "\t";
	cout << "分数:" << stu2.score << endl;*/
	//4.匿名结构体1使用
	stu3.name="xiaoli";
	stu3.age = 21;
	stu3.score = 31;
	cout << "姓名:" << stu3.name << "\t";
	cout << "年龄:" << stu3.age << "\t";
	cout << "分数:" << stu3.score << endl;


	return 0;

}

总结:

  1. 结构体定义的语法:struct 结构体名{成员列表};
  2. 定义结构体三种方式:1:先定义结构体类型,再定义结构体变量 2:先定义结构体类型,再定义结构体变量,同时赋值 3:定义结构体类型的同时,定义结构体变量
  3. 注意事项:在编译时不会为类型分配空间,只为变量分配;创建结构体变量时关键字struct可以省,定义时不能省,结构体变量可以通过.访问成员

结构体数组

#include<iostream>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
int main() {
	//1.定义结构体数据数组(语法同之前所学)
	//语法:数据类型 数组名[数组长度={初始值列表
	struct student stus[3] = {
		{"youliangyu0",23,99},
		{"youliangyu1",23,99},
		{"youliangyu3",23,99},
	};
	//遍历结构体数组(同数组的遍历
	for (int i = 0; i < 3; i++) {
		cout << stus[i].name << " " << stus[i].age << " " << stus[i].score << endl;
		}
	return 0;
}

结构体指针

#include<iostream>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
int main() {
	//1.创建结构体变量
	struct student stu = { "xaiohui",22,22 };
	//结构体指针就是指向指向结构体变量的指针
	struct student* p = &stu;
	//2.通过指针如何访问结构成员
	//2.(*结构体指针).成员名
	(*p).name = "youliangyu";
	cout << "姓名:" << (*p).name << "\t";
	cout << "年龄:" << (*p).age << "\t";
	cout << "分数:" << (*p).score << endl;
//2.2指针->成员
	p->name = "尤良玉";
	cout << "姓名:" << p->name<< "\t";
	cout << "年龄:" << p->age << "\t";
	cout << "分数:" << p->score << endl;
	return 0;
}

结构体做函数参数

#include<iostream>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
//1.函数参数之值传递
void showStudent(struct student stu) {
	stu.name = "小米";
	cout << "===============showStduent================" << endl;
	cout << "姓名:" << stu.name;
	cout << "  年龄:" << stu.age;
	cout << "  分数: " << stu.score<<endl;

}
//2.函数1参数之地址传递
void printStudent2(const struct student* p) {
	//p->name = "老王";//加了const,就会导致修改操作失败
	cout << "===============showStduent2================" << endl;
	cout << "姓名:" << p->name;
	cout << "  年龄:" << p->age;
	cout << "  分数: " << p->score << endl;
}
int main() {
	//1.创建结构体变量
	struct student stu = { "尤良玉",24,88 };
	struct student stu2 = { "尤良玉",24,88 };
	showStudent(stu);
	cout << "===============main================" << endl;
	cout << "姓名:" << stu.name;
	cout << "  年龄:" << stu.age;
	cout << "  分数: " << stu.score<< endl;
	printStudent2(&stu2);
	cout << "===============main================" << endl;
	cout << "姓名:" << stu2.name;
	cout << "  年龄:" << stu2.age;
	cout << "  分数: " << stu2.score;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值