鸟语天空
向函数传递结构
post by:追风剑情 2020-1-19 19:49

示例

//Visual Studio中加上这句才可以使用scanf()
//否则只能使用scanf_s()
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//malloc()、free()
#include <stdlib.h>
#include <time.h>

#define FUNDLEN 50

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

double sum(double, double);
double sum1(const struct funds *); /* 参数是一个指针 */
double sum2(struct funds moolah); /* 参数是一个结构 */

//argc: 参数个数 argv[]: 参数数组
//int main(int argc, char **argv)
int main(int argc, char *argv[])
{
	struct funds stan = {
		"Garlic-Melon Bank",
		4032.27,
		"Lucky's Savings and Loan",
		8543.94
	};

	// 传递结构成员
	printf("Stan has a total of $%.2f.\n",
		sum(stan.bankfund, stan.savefund));

	// 传递结构的地址
	printf("Stan has a total of $%.2f.\n",
		sum1(&stan));

	// 传递结构
	printf("Stan has a total of $%.2f.\n",
		sum2(stan));

	system("pause");
	return 0;
}

double sum(double x, double y)
{
	return (x + y);
}

double sum1(const struct funds * money)
{
	return (money->bankfund + money->savefund);
}

double sum2(struct funds moolah)
{
	return (moolah.bankfund + moolah.savefund);
}

运行测试

1111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容