使用结构数组的函数

作者:追风剑情 发布于:2020-3-11 14:11 分类:C

示例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

#define FUNDLEN 50
#define N 2

struct funds
{
	char bank[FUNDLEN];
	double bankfund;
	char save[FUNDLEN];
	double savefund;
};

double sum(const struct funds money[], int n);

int main(int argc, char* argv[])
{
	struct funds jones[N] = {
		{
			"Garlic-Melon Bank",
			4032.27,
			"Lucky's Savings andLoan",
			8543.94
		},
		{
			"Honest Jack's Bank",
			3620.88,
			"Party Time Savings",
			3802.91
		},
	};

	printf("The Joneses have a total of $%.2f.\n", sum(jones, N));
	//数组名等价于数组首地址
	printf("The Joneses have a total of $%.2f.\n", sum(&jones[0], N));

	system("pause");
	return 0;
}

double sum(const struct funds money[], int n)
{
	double total;
	int i;
	for (i = 0, total = 0; i < n; i++)
		total += money[i].bankfund + money[i].savefund;
	return total;
}

运行测试

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号