这里我们还是解决前面个的那个问题,不够完美吧结构的地址做参数看看。因为函数要处理funds结构所以它也要使用funds声明。
———————————————————————————————————————————————————
#include <stdio.h>
#define FUNDLEN 50
struct funds { /* 定义一个结构并使用标记*/
char bank[FUNDLEN];
double bankfund;
char save[FUNDLEN];
double savefund;
};
double sum (const struct funds *); /* 直接定义函数的参数为一个只想结构的指针 */
/* 这样就可以用->或者.运算符来直接对结构成员进行操作 */
int main (void)
{
struct funds stan = { /* 定义一个结构变量并初始化 */
"Garlic-Melon Bank",
3024.72,
"Lucky's Savings and Loan",
9237.11
};
printf ("Stan has a total of $%.2f \n",sum(&stan));
return 0;
}
double sum (const struct funds * money)
{
return (money->bankfund + money->savefund);
——————————————————————————————————————————————————
这个程序也会产生一样的输出:
stan has a lotal of $12261.83
sum()函数用一个指向fund结构的指针(money)作为他唯一的参数。吧地址&stan传递给改函数是指针money只想结构stan.然后,使用->运算符来获取stan.bankfund和stan.savefund的值,因为函数没有改变所指向的值的内容,所以他吧money声明为一个指向const的指针
索然我们没有这么使用名单上这个函数也可以访问结构的其他成员。注意,必须使用&运算符才能得到结构的地址,和数组名不一样,单独的结构名不是该结构地址的同义词