AliceJames 发表于 2021-4-10 14:50:28

结构体指针数组构建图书馆

本帖最后由 AliceJames 于 2021-4-10 15:04 编辑

目标:创建一个结构体指针数组作为图书馆,让用户可以录入书籍信息并且选择任意一本书打印其信息

我用一个数组来存放结构体,可以正常运行{:10_298:} #include <stdio.h>
#include <stdlib.h>

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

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

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

//录入一本书的书籍信息
void getInput(struct Book *book)
{
      printf("title: ");
      scanf("%s", book->title);
      printf("author: ");
      scanf("%s", book->author);
      printf("price: ");
      scanf("%f", &book->price);
      printf("date: ");
      scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
      printf("publisher: ");
      scanf("%s", book->publisher);
}

//打印一本书的书籍信息
void printBook(struct Book *book)
{
      printf("title: %s\n", book->title);
      printf("author: %s\n", book->author);
      printf("price: %.2f\n", book->price);
      printf("date: %u\n", book->date.year, book->date.month, book->date.day);
      printf("publisher: %s\n", book->publisher);
}

int main(void)
{
      int count = 1, zhiling, temp, i;
      struct Book *b = NULL;//将b定义为结构体指针

      b = (struct Book *)malloc(sizeof(struct Book));
      if(b == NULL)
      {
                printf("Failed!\n");
                exit(1);
      }

        //先录入第一本书
      printf("Write the first book!\n");
      getInput(&b);

      do
      {
                //让用户输入指令
                printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
                scanf("%d", &zhiling);

                switch(zhiling)
                {
                        case 0:{
                              free(b);
                              printf("Thanks for use!\n");
                              return 0;
                        }break;

                        case 1:{
                              count++;
                              b = (struct Book *)realloc(b, count * sizeof(struct Book));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
                              if(b == NULL)
                              {
                                        printf("Failed!\n");
                                        exit(1);
                              }

                              printf("This is No.%d book!\n", count);
                              getInput(&b);
                        }break;

                        case 2:{
                                //打印已录入书籍的书名,以供用户选择查看哪本书
                              for(i = 0; i < count; i++)
                              {
                                        printf("No.%d book: %s\n", i + 1, b.title);
                              }
                              printf("Which one do you want?");
                              scanf("%d", &temp);

                              printBook(&b);
                        }break;
                }
                putchar('\n');
      }while(1);

      return 0;
}

将数组元素换成结构体指针,在调用getInput函数时出现段错误(Segmentation fault)。。。请问如何解决?{:9_234:} #include <stdio.h>
#include <stdlib.h>

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

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

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

void getInput(struct Book *book)
{
      printf("title: ");
      scanf("%s", book->title);
      printf("author: ");
      scanf("%s", book->author);
      printf("price: ");
      scanf("%f", &book->price);
      printf("date: ");
      scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
      printf("publisher: ");
      scanf("%s", book->publisher);
}

void printBook(struct Book *book)
{
      printf("title: %s\n", book->title);
      printf("author: %s\n", book->author);
      printf("price: %.2f\n", book->price);
      printf("date: %u\n", book->date.year, book->date.month, book->date.day);
      printf("publisher: %s\n", book->publisher);
}

int main(void)
{
      int count = 1, zhiling, temp, i;
      struct Book **b = NULL;//将b定义为指向结构体指针数组的指针

      b = (struct Book **)malloc(sizeof(struct Book *));
      if(b == NULL)
      {
                printf("Failed!\n");
                exit(1);
      }

      printf("Write the first book!\n");
      getInput(b);

      do
      {
                printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
                scanf("%d", &zhiling);

                switch(zhiling)
                {
                        case 0:{
                              free(b);
                              printf("Thanks for use!\n");
                              return 0;
                        }break;

                        case 1:{
                              count++;
                              b = (struct Book **)realloc(b, count * sizeof(struct Book *));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
                              if(b == NULL)
                              {
                                        printf("Failed!\n");
                                        exit(1);
                              }

                              printf("This is No.%d book!\n", count);
                              getInput(b);
                        }break;

                        case 2:{
                                //打印已录入书籍的书名,以供用户选择查看哪本书
                              for(i = 0; i < count; i++)
                              {
                                        printf("No.%d book: %s\n", i + 1, b->title);
                              }
                              printf("Which one do you want?");
                              scanf("%d", &temp);

                              printBook(b);
                        }break;
                }
                putchar('\n');
      }while(1);

      return 0;
}

chxchxkkk 发表于 2021-4-11 00:28:31

void getInput(struct Book *);
void printBook(struct Book *);
换成
void getInput(struct Book **);
void printBook(struct Book **);
试试看

AliceJames 发表于 2021-4-11 15:29:53

chxchxkkk 发表于 2021-4-11 00:28
void getInput(struct Book *);
void printBook(struct Book *);
换成


改了之后还是段错误{:9_221:}
是我改的不对吗{:9_222:}#include <stdio.h>
#include <stdlib.h>

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

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

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

void getInput(struct Book **book)//改
{
      printf("title: ");
      scanf("%s", (*book)->title);
      printf("author: ");
      scanf("%s", (*book)->author);
      printf("price: ");
      scanf("%f", &(*book)->price);
      printf("date: ");
      scanf("%d-%d-%d", &(*book)->date.year, &(*book)->date.month, &(*book)->date.day);
      printf("publisher: ");
      scanf("%s", (*book)->publisher);
}

void printBook(struct Book **book)//改
{
      printf("title: %s\n", (*book)->title);
      printf("author: %s\n", (*book)->author);
      printf("price: %.2f\n", (*book)->price);
      printf("date: %u\n", (*book)->date.year, (*book)->date.month, (*book)->date.day);
      printf("publisher: %s\n", (*book)->publisher);
}

int main(void)
{
      int count = 1, zhiling, temp, i;
      struct Book **b = NULL;//将b定义为指向结构体指针数组的指针

      b = (struct Book **)malloc(sizeof(struct Book *));
      if(b == NULL)
      {
                printf("Failed!\n");
                exit(1);
      }

      printf("Write the first book!\n");
      getInput(&b);//改

      do
      {
                printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
                scanf("%d", &zhiling);

                switch(zhiling)
                {
                        case 0:{
                              free(b);
                              printf("Thanks for use!\n");
                              return 0;
                        }break;

                        case 1:{
                              count++;
                              b = (struct Book **)realloc(b, count * sizeof(struct Book *));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
                              if(b == NULL)
                              {
                                        printf("Failed!\n");
                                        exit(1);
                              }

                              printf("This is No.%d book!\n", count);
                              getInput(&b);//改
                        }break;

                        case 2:{
                              //打印已录入书籍的书名,以供用户选择查看哪本书
                              for(i = 0; i < count; i++)
                              {
                                        printf("No.%d book: %s\n", i + 1, b->title);
                              }
                              printf("Which one do you want?");
                              scanf("%d", &temp);

                              printBook(&b);//改
                        }break;
                }
                putchar('\n');
      }while(1);

      return 0;
}

guyihui 发表于 2022-1-14 00:32:51

你全程没有给结构体指针赋相应的结构体空间,肯定会出现段错误啊
页: [1]
查看完整版本: 结构体指针数组构建图书馆