结构体学习(2)----结构体进阶

写在前面

前面我们已经讲过了结构体的基础,结构体的概念、基本使用方法以及结构体的大小都进行了介绍,下面就要谈到结构体在函数中的使用,作为一种构造数据类型,其使用方法也不是那么高深莫测,可以通过尝试对比一下与基础数据类型的用法,学会结构体在函数中的使用。

结构体变量传参

传参我们知道,值传参、指针传参和引用传参,结构体变量作为函数参数进行传参操作也一样的,只不过结构体变量作为参数的时候,函数中参数的个数看着会减少一些,使得整体代码更加简洁明了。

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

struct Car   //定义结构体Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
};
void showCar(Car test)  //结构体变量作为参数
{
	cout << "汽车的价格:" << test.Cprice << endl;
	cout << "汽车的名字:" << test.Cname << endl;
	cout << "汽车的性能:" << test.Cfunction << endl;
	cout << "汽车的燃料:" << test.Cfuel << endl;
}

void main()  
{
	Car car1;
	car1.Cprice = 10;
	car1.Cname = "宝马";
	car1.Cfunction = 'A';
	strcpy_s(car1.Cfuel, "汽油");

	showCar(car1);      //调用函数showCar,以结构体变量car1作为参数
}

在上面的程序中,以结构体变量car1作为参数,如果不这样的话,就需要将car1中的四个成员作为参数进行传递。

还有一种是结构体指针进行参数传递,这样的传递方式和上面的比较类似,只不过是在执行方式上要比上面的方式要快得多。将上面的改成指针传递的方式,如下:

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

struct Car   //定义结构体Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
};
void showCar(Car* test)  //结构体变量作为参数
{
	cout << "汽车的价格:" << test->Cprice << endl;
	cout << "汽车的名字:" << test->Cname << endl;
	cout << "汽车的性能:" << test->Cfunction << endl;
	cout << "汽车的燃料:" << test->Cfuel << endl;
}

void main()  
{
	Car car1;
	car1.Cprice = 10;
	car1.Cname = "宝马";
	car1.Cfunction = 'A';
	strcpy_s(car1.Cfuel, "汽油");

	showCar(&car1);      //调用函数showCar,以结构体变量car1作为参数
}

结构体数组

数组我们已经是很熟悉的了,前面也有所介绍,数组中的元素类型都是一样的。结构体数组也是这样的,结构体数组中的每一个元素都是一个结构体变量,每一个结构体变量都包含着结构体的成员。不管怎样,结构体数组的本质还是一个数组,我们首先来看对结构体数组的声明。

结构体数组的声明有两种方式,可以在定义结构体的时候一并声明,也可以在定义好结构体之后使用结构体变量声明,还有一种就是直接对结构体数组进行声明而结构体的类型名称不需要定义。

一、定义结构体时声明:

struct Car   //定义结构体Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
}car[3];

二、定义结构体之后声明

struct Car   //定义结构体Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
};
Car car[3];

 三、不定义结构体类型名称直接声明数组

struct    
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
}car[3];

举个例子,看一下结构体数组到底是怎样方便了我们。

#include<iostream>
#include<string>

using namespace std;

struct Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
};
Car car[3];

void main()
{
	for (int i = 0; i < 3; i++)
	{
		cout << "输入第" << i+1 << "个车辆的信息" << endl;
		cout << "请输入车辆名字:" << endl;
		cin >> car[i].Cname;
		cout << "请输入车辆价格:" << endl;
		cin >> car[i].Cprice;
		cout << "请输入车辆性能:" << endl;
		cin >> car[i].Cfunction;
		cout << "请输入车辆耗油种类:" << endl;
		cin >> car[i].Cfuel;	

		cout << endl;
	}
	for (int i = 0; i < 3; i++)
	{
		cout << "第" << i+1 << "辆车的信息" << endl;
		cout << car[i].Cname << endl;
		cout << car[i].Cprice << endl;
		cout << car[i].Cfunction << endl;
		cout << car[i].Cfuel << endl;
		cout << endl;
	}
}

这段代码的要求是需要对3辆车的参数信息进行输入并显示出来,如果一辆辆车都来进行代码编写就会很麻烦,显得代码冗长复杂,累赘多余,因此通过使用结构体数组,达到精简代码的作用,即使是100个,1000个车,代码依旧这么长就足够了。运行结果是这样的,将之后的车辆信息输入完毕之后将输入过的车辆信息打印出来。

 指针访问结构体数组

指针变量可以指向一个结构数组,这个结构指针变量的值是结构体数组的首地址,这一点跟普通类型的数组是一样的。将上面的例子改成指针访问结构体数组的方式,如下所示,这段代码的实现功能和上面的代码是一模一样的。

#include<iostream>
#include<string>

using namespace std;

struct Car
{
	int Cprice;
	string Cname;
	char Cfunction;
	char Cfuel[20];
};
Car car[3];

void main()
{
	Car* pCar;  //指针用来输如数据
	pCar = car;
	for (int i = 0; i < 3; i++, pCar++)
	{
		cout << "输入第" << i + 1 << "个车辆的信息" << endl;
		cout << "请输入车辆名字:" << endl;
		cin >> pCar->Cname;
		cout << "请输入车辆价格:" << endl;
		cin >> pCar->Cprice;
		cout << "请输入车辆性能:" << endl;
		cin >> pCar->Cfunction;
		cout << "请输入车辆耗油种类:" << endl;
		cin >> pCar->Cfuel;

		cout << endl;
	}
	Car* poCar;  //指针用来输出之前输入的数据
	poCar = car;
	for (int i = 0; i < 3; i++,poCar++)
	{
		cout << "第" << i + 1 << "辆车的信息" << endl;
		cout << poCar->Cname << endl;
		cout << poCar->Cprice << endl;
		cout << poCar->Cfunction << endl;
		cout << poCar->Cfuel << endl;
		cout << endl;
	}
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值