鱼C论坛

 找回密码
 立即注册
查看: 1520|回复: 3

c第44个视频传递结构体指针

[复制链接]
发表于 2021-12-12 18:21:57 | 显示全部楼层 |阅读模式
10鱼币
  1. #include <stdio.h>
  2. //事先定义结构体
  3. struct Book
  4. {
  5.         char title[10];
  6.         float price;
  7. };

  8. struct Book getInput(struct Book book);
  9. void printBook(struct Book book);//为什么需要这个声明?
  10.                                  //而且输入和输出函数为什么类型还不一样?


  11. //完成数据输入
  12. void getInput(struct Book *book)
  13. {
  14.         printf("请输入书名: ");
  15.         scanf("%s",book->title);
  16.         printf("请输入售价: ");
  17.         scanf("%f",&book->price);
  18. }
  19. //完成数据输出
  20. void printBook(struct Book *book)
  21. {
  22.         printf("书名:%s\n",book->title);
  23.         printf("售价:%f\n",book->price);
  24. }

  25. //主函数进行使用
  26. int main()
  27. {
  28.         struct Book b1,b2;//不应该定义为 struct Book *b1,*bk2吗?
  29.        
  30.         printf("请录入第一本书的信息...\n");
  31.         getInput(&b1);//为什么传&b1过去,不应该传指针*b1过去吗?
  32.         putchar('\n');
  33.         printf("请输入第二本书的信息...\n");
  34.         getInput(&b2);
  35.        
  36.         printf("\n\n录入完毕,现在开始打印验证...\n\n");
  37.         printf("打印第一本书的信息是...\n");
  38.         printBook(&b1);
  39.         putchar('\n');
  40.         printf("打印第二本书的信息是:...\n");
  41.         printBook(&b2);
  42.        
  43.         return 0;
  44. }
复制代码

问题如注释,请高手指点,谢谢!

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

使用道具 举报

发表于 2021-12-12 18:25:58 | 显示全部楼层
  1. struct Book getInput(struct Book book);
  2. void printBook(struct Book book);//为什么需要这个声明?
  3.                                  //而且输入和输出函数为什么类型还不一样?
复制代码


完全不需要这两个声明,写了这两个声明反而是错的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-12 18:27:02 | 显示全部楼层
  1. $ cat main.c
  2. #include <stdio.h>
  3. //事先定义结构体
  4. struct Book
  5. {
  6.         char title[10];
  7.         float price;
  8. };

  9. struct Book getInput(struct Book book);
  10. void printBook(struct Book book);//为什么需要这个声明?
  11.                                  //而且输入和输出函数为什么类型还不一样?


  12. //完成数据输入
  13. void getInput(struct Book *book)
  14. {
  15.         printf("请输入书名: ");
  16.         scanf("%s",book->title);
  17.         printf("请输入售价: ");
  18.         scanf("%f",&book->price);
  19. }
  20. //完成数据输出
  21. void printBook(struct Book *book)
  22. {
  23.         printf("书名:%s\n",book->title);
  24.         printf("售价:%f\n",book->price);
  25. }

  26. //主函数进行使用
  27. int main()
  28. {
  29.         struct Book b1,b2;//不应该定义为 struct Book *b1,*bk2吗?

  30.         printf("请录入第一本书的信息...\n");
  31.         getInput(&b1);//为什么传&b1过去,不应该传指针*b1过去吗?
  32.         putchar('\n');
  33.         printf("请输入第二本书的信息...\n");
  34.         getInput(&b2);

  35.         printf("\n\n录入完毕,现在开始打印验证...\n\n");
  36.         printf("打印第一本书的信息是...\n");
  37.         printBook(&b1);
  38.         putchar('\n');
  39.         printf("打印第二本书的信息是:...\n");
  40.         printBook(&b2);

  41.         return 0;
  42. }
  43. $ gcc-debug -o main main.c
  44. main.c:15:6: error: conflicting types for ‘getInput’; have ‘void(struct Book *)’
  45.    15 | void getInput(struct Book *book)
  46.       |      ^~~~~~~~
  47. main.c:9:13: note: previous declaration of ‘getInput’ with type ‘struct Book(struct Book)’
  48.     9 | struct Book getInput(struct Book book);
  49.       |             ^~~~~~~~
  50. main.c:23:6: error: conflicting types for ‘printBook’; have ‘void(struct Book *)’
  51.    23 | void printBook(struct Book *book)
  52.       |      ^~~~~~~~~
  53. main.c:10:6: note: previous declaration of ‘printBook’ with type ‘void(struct Book)’
  54.    10 | void printBook(struct Book book);//为什么需要这个声明?
  55.       |      ^~~~~~~~~
  56. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-12-15 22:50:31 | 显示全部楼层
人造人 发表于 2021-12-12 18:25
完全不需要这两个声明,写了这两个声明反而是错的

这是小甲鱼视频里有的,但我也觉得没有必要
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 09:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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