c++之结构体

结构体的定义和使用

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

//结构体:属于用户自定义的数据类型,允许用户储存不同的数据类型 
//自定义数据类型,一些类型集合组成的一个类型
//语法:struct 类型名称{成员列表} 
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
struct Student 
{
	//成员列表: 
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }s3; 

//2.通过学生类型创建具体学生 

int main(){
	struct Student s1;
	//通过给s1赋值,用.访问结构体变量中的属性 
	s1.name="张三";
	s1.age=18;
	s1.score=100;
	cout<<"名字"<<s1.name<<"年龄"<<s1.age<<"成绩"<<s1.score<<endl;
	
	struct Student s2={"李四",19,80 };
	cout<<"名字"<<s2.name<<"年龄"<<s2.age<<"成绩"<<s2.score<<endl;
	//在定义时顺便创建变量 
	s3.name="王五";
	s3.age=19;
	s3.score=90;
	cout<<"名字"<<s3.name<<"年龄"<<s3.age<<"成绩"<<s3.score<<endl;
	return 0;
}

结构体数组

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

//结构体数组 
struct Student 
{
	//成员列表: 
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }; 

int main(){
	struct Student arr[3]=
	{
		{"张三",18,100},
		{"李四", 19,80},
		{"王五", 19,90}
	};
	//给结构体中的元素赋值 
	arr[2].name="赵六";
	arr[2].age=22;
	arr[2].score=99; 
	
	for(int i=0;i<3;i++)
	{
		cout<<"名字"<<arr[i].name<<"年龄"<<arr[i].age<<"成绩"<<arr[i].score<<endl;
	}
	
	return 0;
}

结构体指针

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

//结构体指针
struct Student 
{
	//成员列表: 
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }; 


int main(){
	struct Student s={"张三",18,100};
	
	//通过指针指向结构体变量
	Student*p=&s; //注意大小写 
	cout<<"姓名"<<p->name<<"年龄"<<p->age<<"分数"<<p->score<<endl;
	
	return 0;
}

结构体嵌套结构体

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

//嵌套结构体
struct Student 
{
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }; 
 struct teacher 
{
	int id;//教师编号 
	string name;//姓名
	int age;//年龄
	struct Student s;//学生 
 }; 


int main(){
	teacher t;//前面可加struct 
	t.id=122222;
	t.name="老王";
	t.age=30;
	t.s.name="小王";
	t.s.age=18;
	t.s.score=80;
	
	cout<<"老师姓名"<<t.name<<"  老师编号:"<<t.id<<" 老师年龄:" <<t.age<<"  老师学生名字:"<<t.s.name<<" 学生年龄:"<<t.s.age<<" 学生成绩:"<<t.s.score<<endl;
	
	return 0;
}

结构体做函数参数

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

//结构体做函数参数 
struct Student 
{
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }; 
 //1.值传递 
void printstudent(struct Student s)
{
	s.age=120;//不能改变外部变量 
	cout<<"函数中打印学生名字:"<<s.name<<" 学生年龄:"<<s.age<<" 学生成绩:"<<s.score<<endl;

}
//2.地址传递
void printstudent2(struct Student*p)
{
	p->score=100;//可以改变外部变量 
	cout<<"函数中打印学生名字:"<<p->name<<" 学生年龄:"<<p->age<<" 学生成绩:"<<p->score<<endl;

} 
int main(){
	Student s; 
	s.name="小王";
	s.age=18;
	s.score=80;
	
	printstudent(s);
	printstudent2(&s); 
	cout<<"函数中打印学生名字:"<<s.name<<" 学生年龄:"<<s.age<<" 学生成绩:"<<s.score<<endl;
	return 0;
}

const使用场景

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

//const使用场景 
struct Student 
{
	//姓名
	string name;
	//年龄
	int age;
	//分数 
	int score;
 }; 

void printstudent(const Student*s)//加const防止函数体中的误操作 
{
	//s->score=100;不能操作,因为加了const修饰 
	cout<<"函数中打印学生名字:"<<s->name<<" 学生年龄:"<<s->age<<" 学生成绩:"<<s->score<<endl;

}

int main(){
	Student s; 
	s.name="小王";
	s.age=18;
	s.score=80;
	
	printstudent(&s);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值