使用结构数组的函数

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

示例

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5. #define FUNDLEN 50
  6. #define N 2
  7.  
  8. struct funds
  9. {
  10. char bank[FUNDLEN];
  11. double bankfund;
  12. char save[FUNDLEN];
  13. double savefund;
  14. };
  15.  
  16. double sum(const struct funds money[], int n);
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. struct funds jones[N] = {
  21. {
  22. "Garlic-Melon Bank",
  23. 4032.27,
  24. "Lucky's Savings andLoan",
  25. 8543.94
  26. },
  27. {
  28. "Honest Jack's Bank",
  29. 3620.88,
  30. "Party Time Savings",
  31. 3802.91
  32. },
  33. };
  34.  
  35. printf("The Joneses have a total of $%.2f.\n", sum(jones, N));
  36. //数组名等价于数组首地址
  37. printf("The Joneses have a total of $%.2f.\n", sum(&jones[0], N));
  38.  
  39. system("pause");
  40. return 0;
  41. }
  42.  
  43. double sum(const struct funds money[], int n)
  44. {
  45. double total;
  46. int i;
  47. for (i = 0, total = 0; i < n; i++)
  48. total += money[i].bankfund + money[i].savefund;
  49. return total;
  50. }

运行测试

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号