| 
 | 
 
 
发表于 2021-12-12 18:27:02
|
显示全部楼层
 
 
 
- $ cat main.c
 
 - #include <stdio.h>
 
 - //事先定义结构体
 
 - struct Book
 
 - {
 
 -         char title[10];
 
 -         float price;
 
 - };
 
  
- struct Book getInput(struct Book book);
 
 - void printBook(struct Book book);//为什么需要这个声明?
 
 -                                  //而且输入和输出函数为什么类型还不一样?
 
  
 
- //完成数据输入
 
 - void getInput(struct Book *book)
 
 - {
 
 -         printf("请输入书名: ");
 
 -         scanf("%s",book->title);
 
 -         printf("请输入售价: ");
 
 -         scanf("%f",&book->price);
 
 - }
 
 - //完成数据输出
 
 - void printBook(struct Book *book)
 
 - {
 
 -         printf("书名:%s\n",book->title);
 
 -         printf("售价:%f\n",book->price);
 
 - }
 
  
- //主函数进行使用
 
 - int main()
 
 - {
 
 -         struct Book b1,b2;//不应该定义为 struct Book *b1,*bk2吗?
 
  
-         printf("请录入第一本书的信息...\n");
 
 -         getInput(&b1);//为什么传&b1过去,不应该传指针*b1过去吗?
 
 -         putchar('\n');
 
 -         printf("请输入第二本书的信息...\n");
 
 -         getInput(&b2);
 
  
-         printf("\n\n录入完毕,现在开始打印验证...\n\n");
 
 -         printf("打印第一本书的信息是...\n");
 
 -         printBook(&b1);
 
 -         putchar('\n');
 
 -         printf("打印第二本书的信息是:...\n");
 
 -         printBook(&b2);
 
  
-         return 0;
 
 - }
 
 - $ gcc-debug -o main main.c
 
 - main.c:15:6: error: conflicting types for ‘getInput’; have ‘void(struct Book *)’
 
 -    15 | void getInput(struct Book *book)
 
 -       |      ^~~~~~~~
 
 - main.c:9:13: note: previous declaration of ‘getInput’ with type ‘struct Book(struct Book)’
 
 -     9 | struct Book getInput(struct Book book);
 
 -       |             ^~~~~~~~
 
 - main.c:23:6: error: conflicting types for ‘printBook’; have ‘void(struct Book *)’
 
 -    23 | void printBook(struct Book *book)
 
 -       |      ^~~~~~~~~
 
 - main.c:10:6: note: previous declaration of ‘printBook’ with type ‘void(struct Book)’
 
 -    10 | void printBook(struct Book book);//为什么需要这个声明?
 
 -       |      ^~~~~~~~~
 
 - $
 
  复制代码 |   
 
 
 
 |