shallow丶 发表于 2021-12-16 21:15:00

为什么b1,b2是未初始化局部变量啊

#include<stdio.h>

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

struct Book
{
        char title;
        char author;
        float price;
        struct Date date;
        char publisher;
};

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

#include <stdio.h>

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

struct Book
{
      char title;
      char author;
      float price;
      struct Date date;
      char publisher;
};

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

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

      //return book;
}

//void printBook(struct Book 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);
      */
      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);
      getInput(&b1);
      putchar('\n');
      printf("请录入第二本书的信息:\n");
      //b2 = getInput(b2);
      getInput(&b2);

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

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

      return 0;
}

海的上方是天空 发表于 2021-12-17 10:35:36

定义 b1 , b2 的时候没有初始化啊

shallow丶 发表于 2021-12-17 14:10:52

人造人 发表于 2021-12-16 22:13


还得是指针啊

人造人 发表于 2021-12-17 14:12:50

shallow丶 发表于 2021-12-17 14:10
还得是指针啊

对呀
页: [1]
查看完整版本: 为什么b1,b2是未初始化局部变量啊