C/C++中二维数组作为形参传递参数

问题引出的背景(二维数组的应用案例)

案例描述:由三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩

姓名语文数学英语
张三100100100
李四9050100
王五607080
代码实现思路

设计一个函数,例如:void total_score_of_each_stu(param1, param2, param3,param4),其中param1为二维数组维护的一个成绩单,第一维表示同学的个数,第二位为考试科目数;param2为学生总数;param3为考试科目的总数;param4用来保存所有学生总成绩的一维数组。

引出的问题

param1为二维数组,在函数void total_score_of_each_stu(param1, param2, param3,param4)中该以怎样的形参形式去接收函数调用时实参传递的参数?

解决方法一:形参为二维数组
//@method 1: 形参为二维数组

/*
* @function: Output the total score of earch student
* @parma1[in]: The transcript
* @param2[in]: Number of students
* @param3[in]: Number of disciplines
* @param[out]: The total score of earch student
* @return: NA
* @author: Cohen
* @data: 2022/11/26
*/
void total_score_of_each_stu_01(int transc[][3], int num_stu, int num_discip, int* tal_score)
{
	for (int i = 0; i < num_stu; i++)
	{
		int score = 0;
		for (int j = 0; j < num_discip; j++)
		{
			tal_score[i] += transc[i][j];
		}
	}	
}

void test01()
{
	int transc[3][3] = {
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	string names[3] = { "张三","李四","王五" };

	int num_stu = sizeof(transc) / sizeof(transc[0]);
	int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);

	int tal_score[3] = { 0 };

	total_score_of_each_stu_01(transc, num_stu, num_discip, tal_score);

	for (int i = 0; i < num_stu; i++)
	{
		cout << names[i] << ":  " << tal_score[i] << endl;
	}
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}
解决方法二:形参为指向数组的指针
//@method 2: 形参为指向数组的指针

void total_score_of_each_stu_02(int (*transc)[3], int num_stu, int num_discip, int* tal_score)
{
	for (int i = 0; i < num_stu; i++)
	{
		int score = 0;
		for (int j = 0; j < num_discip; j++)
		{
			tal_score[i] += transc[i][j];
		}
	}
}

void test02()
{
	int transc[3][3] = {
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	string names[3] = { "张三","李四","王五" };

	int num_stu = sizeof(transc) / sizeof(transc[0]);
	int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);

	int tal_score[3] = { 0 };

	total_score_of_each_stu_02(transc, num_stu, num_discip, tal_score);

	for (int i = 0; i < num_stu; i++)
	{
		cout << names[i] << ":  " << tal_score[i] << endl;
	}
}

int main()
{
	test02();

	system("pause");
	return 0;
}
解决方法三:形参为指向指针的指针,即二级指针

注意在调用此方法时,param1必须为指针,而不能为数组名,否则会报错

//@method 3: 形参为指向指针的指针,即二级指针

void total_score_of_each_stu_03(int** transc, int num_stu, int num_discip, int* tal_score)
{
	for (int i = 0; i < num_stu; i++)
	{
		int score = 0;
		for (int j = 0; j < num_discip; j++)
		{
			tal_score[i] += transc[i][j];
		}
	}
}

void test03()
{
	int transc[3][3] = {
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	int* p_transc[3];
	p_transc[0] = transc[0];
	p_transc[1] = transc[1];
	p_transc[2] = transc[2];

	string names[3] = { "张三","李四","王五" };

	int num_stu = sizeof(transc) / sizeof(transc[0]);
	int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);

	int tal_score[3] = { 0 };

	total_score_of_each_stu_03(p_transc, num_stu, num_discip, tal_score);

	for (int i = 0; i < num_stu; i++)
	{
		cout << names[i] << ":  " << tal_score[i] << endl;
	}
}

int main()
{
	test03();

	system("pause");
	return 0;
}
解决方法四:形参为一级指针

如果实参的二维数组的内存空间是malloc或者new出来的,那么可能会出现异常,因为malloc或者new出来的内存空间不一定是连续的,而这种方法transc[i * num_discip + j]访问二维数组的元素,前提是二维数组内存空间物理连续

//@method 4: 形参为一级指针

void total_score_of_each_stu_04(int* transc, int num_stu, int num_discip, int* tal_score)
{
	for (int i = 0; i < num_stu; i++)
	{
		int score = 0;
		for (int j = 0; j < num_discip; j++)
		{
			tal_score[i] += transc[i * num_discip + j];
		}
	}
}

void test04()
{
	int transc[3][3] = {
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	string names[3] = { "张三","李四","王五" };

	int num_stu = sizeof(transc) / sizeof(transc[0]);
	int num_discip = sizeof(transc[0]) / sizeof(transc[0][0]);

	int tal_score[3] = { 0 };

	total_score_of_each_stu_04((int*)transc, num_stu, num_discip, tal_score);

	for (int i = 0; i < num_stu; i++)
	{
		cout << names[i] << ":  " << tal_score[i] << endl;
	}
}

int main()
{
	test04();

	system("pause");
	return 0;
}
总结
  • 方法一和方法二使用的前提是,需要提前知道传入的二维数组的第二维长度,代码的通用性不高,这里列出来只是为了具体说明所提出来的解决办法在代码中的具体应用,辅助理解。
  • 方法二的通用最好,只是在构建二维数组时,我们需要构建一个指向二维数组的二级指针,而不能直接使用二维数组的名称作为实参传递给形参。
  • 方法四通用性仅次于方法三,使用的前提是,二维数组的内存空间不是通过malloc或者new在堆区开辟的内存空间,因为通过这两种方法开辟的内存空间不能够保证二维数组在物理内存上空间连续。

博客下期主题预告:malloc和new的区别,以及通过动态分配的内存,其物理内存空间是否一定连续?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值