C++基础--结构体

1. 概念

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

//结构体:自定义数据类型,
//1.创建学生数据类型:学生包括(姓名,年龄,分数)
struct student
{
	string name;
	int age;
	int score;	
}s3;

//2.通过学生类型创建具体学生
int main()
{
//2.1 struct student s1
	student s1;
	s1.name = "张三";
	s1.age = 18;
	s1.score = 80;
	cout <<"姓名:"<<s1.name<<endl;
	cout <<"年龄:"<<s1.age<<endl;
	cout <<"分数:"<<s1.score<<endl;

//2.2 struct student s2={....}
	student s2={"李四",20,78};
	cout <<"姓名:"<< s2.name<<endl;
	cout <<"年龄:"<<s2.age<<endl;
	cout <<"分数:"<<s2.score<<endl;
//2.3 在定义结构体时顺便创建结构体变量

	system("pause");
	return 0;
}

2. 嵌套结构体

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

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

struct teacher
{
	int id;
	string name;
	int age;
	student stu;
};

int main()
{
	teacher t;
	t.id = 1000;
	t.name="gg";
	t.age = 42;
	t.stu.name ="小明";
	t.stu.age = 23;
	t.stu.score = 80;

	cout <<t.id<<endl;
	cout <<t.name<<endl;
	cout <<t.age<<endl;
	cout <<t.stu.name<<endl;
	cout <<t.stu.age<<endl;
	cout <<t.stu.score<<endl;
	system("pause");
	return 0;
}

3. 结构体数组

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

//struct 结构体名{};
struct student
{
	//成员列表
	string name;
	int age;
	int score;	
};

int main()
{

//创建结构体数组
	struct student arr[3]=
	{
		{"张三",18,60},
		{"李四",20,89},
		{"王文",19,80}
	};
	
	for(int i=0;i<3;i++)
	{
	cout <<"姓名:"<<arr[i].name<<" ";
	cout <<"年龄:"<<arr[i].age<<" ";
	cout <<"分数:"<<arr[i].score<<endl;
	}
	system("pause");
	return 0;
}

4. 结构体做函数参数

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

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

void printstu1(student s)
{
	s.age=100;
	cout << "在printstu1中:" <<endl;
	cout << s.name <<endl;
	cout << s.age <<endl;
	cout << s.score <<endl;
}

void printstu2(student * s)
{
	s->age=100;
	cout << "在printstu2中:" <<endl;
	cout << s->name <<endl;
	cout << s->age <<endl;
	cout << s->score <<endl;
}

int main()
{
//将学生传入到一个参数中,打印学生信息
//1. 创建结构体变量
	student s;
	s.name= "张三";
	s.age=18;
	s.score=84;
	
//值传递,用.访问结构体变量信息
	printstu1(s);
	cout << "主函数中:"<<endl;
	cout << s.name <<endl;
	cout << s.age <<endl;
	cout << s.score <<endl;
// 地址传递 ,用->访问结构体变量信息
	printstu2(&s);
	cout << "主函数中:"<<endl;
	cout << s.name <<endl;
	cout << s.age <<endl;
	cout << s.score <<endl;
	system("pause");
	return 0;
}

5. 结构体指针

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

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

int main()
{
//1.创建学生结构体变量
	student s = { "张三", 18, 20 };
//2.通过指针指向结构体变量
	student * p = &s;
//3.利用指针访问结构体变量中的数据
//必须要用->才能访问
	cout << "姓名:" << p->name << endl;
	cout << "年龄:" << p->age << endl;
	cout << "分数:" << p->score << endl;
	system("pause");
	return 0;
}

6. const使用场景

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

//const的使用场景
struct student
{
	string name;
	int age;
	int score;
};

//将形参改成指针可以节省空间
void printstu(const student * s){
	//s->age = 100; 一旦有修改,就会报错,加入const可以提醒我们误操作
	cout << "姓名:" << s->name << endl;
	cout << "年龄:" << s->age << endl;
	cout << "分数:" << s->score << endl;
}
int main()
{
//1.创建学生结构体变量
	student s = { "张三", 18, 20 };
//2.通过函数打印结构体变量信息
	printstu(&s);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值