鱼C论坛

 找回密码
 立即注册
查看: 1502|回复: 5

[已解决]有关c语言结构体 值传递 给函数的一个问题

[复制链接]
发表于 2020-9-26 12:51:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小甲鱼在带你学c里面的一个值传递让我有点疑惑
我把我写的和甲鱼大大的都贴出来。

我是这样写的,也是成功的:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book getInput(void);

  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book
  11. {
  12.         char name[40];
  13.         char title[128];
  14.         float price;
  15.         struct Date date;
  16. };

  17. struct Book getInput(void)//《《《《《《《《《《疑惑点《《《《《《《《《《《《《
  18. {
  19.         struct Book book;
  20.         printf("请输入书名:");
  21.         scanf("%s",book.title);
  22.         printf("请输入姓名:");
  23.         scanf("%s",book.name);
  24.         printf("请输入售价:");
  25.         scanf("%f",&book.price);
  26.         printf("请输入日期:");
  27.         scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
  28.        
  29.         return book;
  30. }

  31. void printBook(struct Book book)//将结构体变量 值传递 给指针会使开销变大。
  32. {
  33.         printf("name:%s\n",book.name);
  34.         printf("title:%s\n",book.title);
  35.         printf("price:%f\n",book.price);
  36.         printf("date:%d%d%d\n",book.date.year,book.date.month,book.date.day);
  37. }

  38. int main(void) {
  39.         struct Book b1,b2;
  40.         printf("请输入第一本书的信息》》》\n");
  41.         b1=getInput();
  42.         putchar('\n');
  43.         printf("请输入第二本书的信息》》》\n");
  44.         b2=getInput();
  45.         putchar('\n');
  46.         printf("你的录入:\n");
  47.         printBook(b1);
  48.         printBook(b2);
  49.         return 0;
  50. }
复制代码



甲鱼老师是这样写的,但我不知道为什么要把b1值传递给函数,解释不通啊aaa:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book getInput(struct Book book);

  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book
  11. {
  12.         char name[40];
  13.         char title[128];
  14.         float price;
  15.         struct Date date;
  16. };

  17. struct Book getInput(struct Book book)//《《《《《《《《《《《疑惑点《《《《《《《《《
  18. {
  19.         printf("请输入书名:");
  20.         scanf("%s",book.title);
  21.         printf("请输入姓名:");
  22.         scanf("%s",book.name);
  23.         printf("请输入售价:");
  24.         scanf("%f",&book.price);
  25.         printf("请输入日期:");
  26.         scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
  27.        
  28.         return book;
  29. }

  30. void printBook(struct Book book)//将结构体变量 值传递 给指针会使开销变大。
  31. {
  32.         printf("name:%s\n",book.name);
  33.         printf("title:%s\n",book.title);
  34.         printf("price:%f\n",book.price);
  35.         printf("date:%d%d%d\n",book.date.year,book.date.month,book.date.day);
  36. }

  37. int main(void) {
  38.         struct Book b1,b2;
  39.         printf("请输入第一本书的信息》》》\n");
  40.         b1=getInput(b1); //《《《《《《《《《《疑惑点《《《《《《《《《《《《《
  41.         putchar('\n');
  42.         printf("请输入第二本书的信息》》》\n");
  43.         b2=getInput(b2);//《《《《《《《《《《疑惑点《《《《《《《《《《《《《
  44.         putchar('\n');
  45.         printf("你的录入:\n");
  46.         printBook(b1);
  47.         printBook(b2);
  48.         return 0;
  49. }
复制代码
最佳答案
2020-9-27 22:39:50
greenery 发表于 2020-9-27 17:56
我懂了,甲鱼大大的这个代码,把b1值传递给函数局部变量book,同时定义了一个Book结构体类型并且把b1的值传 ...

对啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-26 21:44:31 From FishC Mobile | 显示全部楼层
我也不知道怎么回答,只能说都可以,没毛病,我感觉没必要疑惑!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-9-27 17:36:21 | 显示全部楼层
还是想知道一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-27 17:56:12 | 显示全部楼层
我懂了,甲鱼大大的这个代码,把b1值传递给函数局部变量book,同时定义了一个Book结构体类型并且把b1的值传进去了。
又因为接下去的赋值语句把原b1的值覆盖掉了所以相当于就是只起到定义Book结构体类型的作用。

我的那句就是单纯的定义Book结构体类型,所以两个作用其实一样。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-27 22:39:50 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
greenery 发表于 2020-9-27 17:56
我懂了,甲鱼大大的这个代码,把b1值传递给函数局部变量book,同时定义了一个Book结构体类型并且把b1的值传 ...

对啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-27 22:41:02 From FishC Mobile | 显示全部楼层
还有你要点回复,不然我收不到,我碰巧看到了,不然我都不知道你还发了消息
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-15 07:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表