鱼C论坛

 找回密码
 立即注册
查看: 650|回复: 4

[已解决]为什么b1,b2是未初始化局部变量啊

[复制链接]
发表于 2021-12-16 21:15:00 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>

struct Date
{
        int year;
        int month;
        int day;
};

struct Book
{
        char title[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};

struct Book getInput(struct Book book);
void printBook(struct Book book);

struct Book getInput(struct Book book)
{
        printf("请输入书名:");
        scanf_s("%s", book.title);
        printf("请输入作者:");       
        scanf_s("%s", book.author);
        printf("请输入价格:");
        scanf_s("%.2f", &book.price);
        printf("请输入日期:");
        scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
        printf("请输入出版社:");
        scanf_s("%s", book.publisher);

        return book;
}

void printBook(struct Book book)
{
        printf("书名:%s\n", book.title);
        printf("作者:%s\n", book.author);
        printf("售价:%.2f\n", book.price);
        printf("日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
        printf("出版社:%s\n", book.publisher);
}

int main(void)
{
        struct Book b1, b2;

        printf("请录入第一本书的信息:\n");
        b1 = getInput(b1);
        putchar('\n');
        printf("请录入第二本书的信息:\n");
        b2 = getInput(b2);

        printf("\n\n录入完毕,现在开始打印...\n\n");

        printf("打印第一本书的信息...");
        printBook(b1);
        putchar('\n');
        printf("打印第二本书的信息...");
        printBook(b2);

        return 0;
}
最佳答案
2021-12-16 22:13:45
  1. #include <stdio.h>

  2. struct Date
  3. {
  4.         int year;
  5.         int month;
  6.         int day;
  7. };

  8. struct Book
  9. {
  10.         char title[128];
  11.         char author[40];
  12.         float price;
  13.         struct Date date;
  14.         char publisher[40];
  15. };

  16. //struct Book getInput(struct Book book);
  17. //void printBook(struct Book book);

  18. //struct Book getInput(struct Book book)
  19. //struct Book *getInput(struct Book *book)
  20. void getInput(struct Book *book)
  21. {
  22.         printf("请输入书名:");
  23.         //scanf_s("%s", book.title);
  24.         scanf("%s", book->title);
  25.         printf("请输入作者:");      
  26.         //scanf_s("%s", book.author);
  27.         scanf("%s", book->author);
  28.         printf("请输入价格:");
  29.         //scanf_s("%.2f", &book.price);
  30.         scanf("%2f", &book->price);
  31.         printf("请输入日期:");
  32.         //scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
  33.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  34.         printf("请输入出版社:");
  35.         //scanf_s("%s", book.publisher);
  36.         scanf("%s", book->publisher);

  37.         //return book;
  38. }

  39. //void printBook(struct Book book)
  40. void printBook(struct Book *book)
  41. {
  42.         /*
  43.         printf("书名:%s\n", book.title);
  44.         printf("作者:%s\n", book.author);
  45.         printf("售价:%.2f\n", book.price);
  46.         printf("日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  47.         printf("出版社:%s\n", book.publisher);
  48.         */
  49.         printf("书名:%s\n", book->title);
  50.         printf("作者:%s\n", book->author);
  51.         printf("售价:%.2f\n", book->price);
  52.         printf("日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  53.         printf("出版社:%s\n", book->publisher);
  54. }

  55. int main(void)
  56. {
  57.         struct Book b1, b2;

  58.         printf("请录入第一本书的信息:\n");
  59.         //b1 = getInput(b1);
  60.         getInput(&b1);
  61.         putchar('\n');
  62.         printf("请录入第二本书的信息:\n");
  63.         //b2 = getInput(b2);
  64.         getInput(&b2);

  65.         printf("\n\n录入完毕,现在开始打印...\n\n");

  66.         printf("打印第一本书的信息...");
  67.         //printBook(b1);
  68.         printBook(&b1);
  69.         putchar('\n');
  70.         printf("打印第二本书的信息...");
  71.         //printBook(b2);
  72.         printBook(&b2);

  73.         return 0;
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-16 22:13:45 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. struct Date
  3. {
  4.         int year;
  5.         int month;
  6.         int day;
  7. };

  8. struct Book
  9. {
  10.         char title[128];
  11.         char author[40];
  12.         float price;
  13.         struct Date date;
  14.         char publisher[40];
  15. };

  16. //struct Book getInput(struct Book book);
  17. //void printBook(struct Book book);

  18. //struct Book getInput(struct Book book)
  19. //struct Book *getInput(struct Book *book)
  20. void getInput(struct Book *book)
  21. {
  22.         printf("请输入书名:");
  23.         //scanf_s("%s", book.title);
  24.         scanf("%s", book->title);
  25.         printf("请输入作者:");      
  26.         //scanf_s("%s", book.author);
  27.         scanf("%s", book->author);
  28.         printf("请输入价格:");
  29.         //scanf_s("%.2f", &book.price);
  30.         scanf("%2f", &book->price);
  31.         printf("请输入日期:");
  32.         //scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
  33.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  34.         printf("请输入出版社:");
  35.         //scanf_s("%s", book.publisher);
  36.         scanf("%s", book->publisher);

  37.         //return book;
  38. }

  39. //void printBook(struct Book book)
  40. void printBook(struct Book *book)
  41. {
  42.         /*
  43.         printf("书名:%s\n", book.title);
  44.         printf("作者:%s\n", book.author);
  45.         printf("售价:%.2f\n", book.price);
  46.         printf("日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  47.         printf("出版社:%s\n", book.publisher);
  48.         */
  49.         printf("书名:%s\n", book->title);
  50.         printf("作者:%s\n", book->author);
  51.         printf("售价:%.2f\n", book->price);
  52.         printf("日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  53.         printf("出版社:%s\n", book->publisher);
  54. }

  55. int main(void)
  56. {
  57.         struct Book b1, b2;

  58.         printf("请录入第一本书的信息:\n");
  59.         //b1 = getInput(b1);
  60.         getInput(&b1);
  61.         putchar('\n');
  62.         printf("请录入第二本书的信息:\n");
  63.         //b2 = getInput(b2);
  64.         getInput(&b2);

  65.         printf("\n\n录入完毕,现在开始打印...\n\n");

  66.         printf("打印第一本书的信息...");
  67.         //printBook(b1);
  68.         printBook(&b1);
  69.         putchar('\n');
  70.         printf("打印第二本书的信息...");
  71.         //printBook(b2);
  72.         printBook(&b2);

  73.         return 0;
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2021-12-17 10:35:36 | 显示全部楼层
定义 b1 , b2 的时候没有初始化啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-17 14:10:52 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 14:12:50 | 显示全部楼层

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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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