鱼C论坛

 找回密码
 立即注册
查看: 2324|回复: 1

求大神指教一下,这是哪里的问题,内存分配的问题吗?(我看着小甲鱼的视频打出来....

[复制链接]
发表于 2019-1-4 10:10:31 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>

void getInput(struct Book *book);
void addBook(struct Book **library);
void printfLibrary(struct Book *library);
void releaseLibrary(struct Book **library);

struct Book
{
        char title[120];
        char author[40];
        struct Book *next;
};

void getInput(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s",book->title);
        printf("请输入作者:");
        scanf("%s",book->author);

}

void addBook(struct Book **library)
{
        struct Book *book,*temp;

        book=(struct Book *)malloc(sizeof(struct Book));
        if(book==NULL)
        {
                printf("内存分配失败!");
                exit(1);
        }

        getInput(book);

        if(*library!=NULL)
        {
                temp=*library;
                *library=book;
                book->next=temp;
       
        }
        else
        {
                *library=book;
                book->next=NULL;
        }

}

void printfLibrary(struct Book *library)
{
        struct Book *book;
        int count=1;

        while(book!=NULL)
        {
                printf("Book%d:",count);
                printf("书名:%s",book->title);
                printf("作者:%s",book->author);
                book=book->next;
                count++;
        }
}

void releaseLibrary(struct Book **library)
{
        struct Book *temp;

        while(library!=NULL)
        {
                temp=*library;
                *library=(*library)->next;
                free(temp);
        }
}

int mian(void)
{
        struct Book *library=NULL;
        int ch;

        while(1)
        {
                printf("请问是否需要录入书籍信息(Y/N):");
                do
                {
                        ch=getchar();
                }while(ch!='Y'&&ch!='N');

                if(ch=='Y')
                {
                        addBook(&library);
                }
                else
                {
                        break;
                }
        }

        printf("请问是否需要打印图书信息(Y/N):");
        do
        {
                ch=getchar();
        }while(ch!='Y'&&ch!='N');
        if(ch=='Y')
        {
                printfLibrary(library);
        }

        releaseLibrary(&library);

        addBook(&library);

        return(0);
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-5 12:17:38 | 显示全部楼层
I found 4 problems when watching your code
P 1. Undefined reference to `main'
A 1. Your entry point typed in int mian(void), instead of int main(void)

P2. struct Book{} declared under function()
A 2. When declared' function try to find struct Book that does not declared before, error pops up.

P 3. In printfLibrary() section, book address was empty
A 3. You have to set book = library;

P 4. Write argument name when declare function e.g void addBook(struct Book **xyz);
A 4. When you use the function to print your struct, it may not show the input. whould be write as void addBook(struct Book **);

Code should be like this:
  1. struct Book
  2. {
  3.         char title[120];
  4.         char author[40];
  5.         struct Book *next;
  6. };

  7. void addBook(struct Book **);
  8. void getInput(struct Book *);
  9. void printfLibrary(struct Book *);
  10. void releaseLibrary(struct Book **);

  11. _Bool pCOMMAND(void);
  12. _Bool pDEBUG(void);
  13. _Bool pEND(void);

  14. _Bool debugSwitch = FALSE;
  15. int main(void)
  16. {
  17.         struct Book *library=NULL;
  18.         char ch;

  19.         while(1)
  20.         {
  21.                 printf("请问是否需要录入书籍信息(Y/N):");
  22.                 do
  23.                 {
  24.                         ch=getchar();
  25.                 }while(ch!='Y'&&ch!='N');

  26.                 if(ch=='Y')
  27.                 {
  28.                         addBook(&library);
  29.                 }
  30.                 else
  31.                 {
  32.                         break;
  33.                 }
  34.         }

  35.         printf("请问是否需要打印图书信息(Y/N):");
  36.         do
  37.         {
  38.                 ch=getchar();
  39.         }while(ch!='Y' && ch!='N');
  40.         if(ch == 'Y')
  41.         {
  42.                 printfLibrary(library);
  43.         }

  44.         releaseLibrary(&library);
  45.         return 0;
  46. }
  47. void getInput(struct Book *book)
  48. {
  49.         printf("请输入书名:");
  50.         scanf("%s",book->title);
  51.         printf("请输入作者:");
  52.         scanf("%s",book->author);
  53.         printf("%s\n",book->author);
  54.         return;
  55. }

  56. void addBook(struct Book **library)
  57. {
  58.         struct Book *book;
  59.         struct Book *temp;
  60.         book = (struct Book *)malloc(sizeof(struct Book));
  61.         if(book == NULL)
  62.         {
  63.                 printf("内存分配失败!");
  64.                 exit(1);
  65.         }

  66.         getInput(book);
  67.         if(*library != NULL)
  68.         {
  69.                 temp=*library;
  70.                 *library=book;
  71.                 book->next=temp;

  72.         }
  73.         else
  74.         {
  75.                 *library=book;
  76.                 book->next=NULL;
  77.         }

  78. }

  79. void printfLibrary(struct Book *library)
  80. {
  81.         struct Book *book = null;
  82.         int count = 1;
  83.         book = library;
  84.         while (book != NULL)
  85.         {
  86.                         printf("书名:%s\n",(*book).title);
  87.                         printf("作者:%s\n",(*book).author);
  88.                         book=book->next;
  89.                         count++;
  90.         }
  91. }

  92. void releaseLibrary(struct Book **library)
  93. {
  94.         struct Book *temp;

  95.         while(library!=NULL)
  96.         {
  97.                 temp=*library;
  98.                 *library=(*library)->next;
  99.                 free(temp);
  100.         }
  101. }
复制代码

output

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-15 10:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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