c++基础知识第十天:结构体嵌套结构体,结构体作函数参数

一、结构体嵌套结构体

结构体内的成员可以是另一个结构体(访问时用.访问到不能访问为止)

1、例如:每个老师指导一个学员,一个老师的结构体中嵌套一个学生的结构体

#include<iostream>
using namespace std;
#include<string>
//结构体嵌套结构体

//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 = "老王";
	t.age = 50;
	t.stu.name = "小汪";
	t.stu.age = 18;
	t.stu.score = 100;
	cout << "老师姓名:" << t.name << " " << "老师职工编号:" << t.id << " " << "老师的年龄:" << t.age << endl;
	cout << "老师辅导的学生姓名:" << t.stu.name << " " << "学生的年龄:" << t.stu.age << " " << "学生的分数:" << t.stu.score << endl;
}


二、结构体作为函数参数

1、作用:结构体作为参数向函数传递

2、传递方式:值传递(形参改变,实参不改变),地址传递(实参随着形参的改变而改变)

因为值传递时相当于在给形参开辟一段内存空间并将实参的复制进去,对于形参变量的操作在形参内存空间中进行。但是地址传递形参开辟的空间传入的是实参的地址,对于形参变量的操作会通过地址访问到实参的内存空间,所以实参跟着改变。

1)

#include<iostream>
using namespace std;
#include<string>
//结构体做函数的参数
//将学生传入到一个函数中,打印学生的信息

//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
	string name;
	int age;
	int score;
};
//3、定义打印函数
void printfunction(student s)
{
	cout << s.name << " " << s.age << " " << s.score << endl;
}


int main()
{
	//2、创建结构体变量
	student s1 = { "张三",24,100 };
	printfunction(s1);//调用函数

}


2)指针用箭头访问属性

#include<iostream>
using namespace std;
#include<string>
//结构体做函数的参数
//将学生传入到一个函数中,打印学生的信息

//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
	string name;
	int age;
	int score;
};
//3、定义打印函数
void printfunction(student *p1)
{
	cout << p1->name << " " << p1->age << " " << p1->score << endl;
}


int main()
{
	//2、创建结构体变量
	student s1 = { "张三",24,100 };
	student* p = &s1;
	printfunction(p);//调用函数

}


值传递和地址传递区别:

值传递时在,在子函数中改变属性(也就是改变了形参),不会改变主函数中的实参的输出。

#include<iostream>
using namespace std;
#include<string>
//结构体做函数的参数
//将学生传入到一个函数中,打印学生的信息

//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
	string name;
	int age;
	int score;
};
//3、定义打印函数
void printfunction(student s)
{
	s.age = 100;
	cout <<"值传递子函数中:"<< s.name << " " << s.age << " " << s.score << endl;
	//cout << p1->name << " " << p1->age << " " << p1->score << endl;
}


int main()
{
	//2、创建结构体变量
	student s1 = { "张三",24,100 };
	//student* p = &s1;
	printfunction(s1);//调用函数
	cout<<"main函数中:"<< s1.name << " " << s1.age << " " << s1.score << endl;
}


地址传递时,实参随着形参的改变而改变

#include<iostream>
using namespace std;
#include<string>
//结构体做函数的参数
//将学生传入到一个函数中,打印学生的信息

//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
	string name;
	int age;
	int score;
};
//3、定义打印函数
void printfunction(student *p1)
{
	p1->age = 100;
	//cout <<"值传递子函数中:"<< s.name << " " << s.age << " " << s.score << endl;
	cout <<"地址传递时:"<< p1->name << " " << p1->age << " " << p1->score << endl;
}


int main()
{
	//2、创建结构体变量
	student s1 = { "张三",24,100 };
	student* p = &s1;
	printfunction(p);//调用函数
	cout<<"main函数中:"<< s1.name << " " << s1.age << " " << s1.score << endl;
}


三、结构体中const使用场景

1、作用:用const防止误操作(当代码过大,若进行限制,就会提示你是否进行误操作)

#include<iostream>
using namespace std;
#include<string>
//结构体const使用场景


//1、定义结构体(先告诉系统有这样一个数据类型)
struct student
{
	string name;
	int age;
	int score;
};

void printsudent(const student *s)//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
{
	错误:s->age = 100;//如果这里将形参的属性修改,那么实参也会被修改.加入const后,一旦有修改的操作就会报错,可以防止误操作
	cout << s->name << " " << s->age << " " << s->score<<endl;
}

int main()
{//2、在main函数中创建结构体变量并初始化
	struct student s = { "张三",15,70 };
	//通过函数打印结构体变量信息
	printsudent(&s);
	cout << "张三的年龄:" << s.age << endl;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值