鱼C论坛

 找回密码
 立即注册
查看: 1812|回复: 2

[已解决]关于带你学C带你飞第46课链表free释放的问题

[复制链接]
发表于 2019-9-25 18:24:27 | 显示全部楼层 |阅读模式

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

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

x
这个程序我在下面freelibrary(struct Book *library)函数声明的时候用了一个*library没又用两个**号一个型号在我的理解中不也是调用了main函数中的library指针并对他进行改变么?并且我要释放的是这个指针占用的空间并不是更改里边的数据我感觉一个*号应该够了的,但是在小甲鱼的视频中用的是两个**我想知道为什么,并且我在这里用一个型号也是能释放我所申请的内存地址的编译器也没有报错,那位大神能给讲解一下谢谢
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define MAX1 128
  4. #define MAX2 40

  5. struct Book
  6. {
  7.                 char title[MAX1];
  8.                 char author[MAX2];
  9.                 struct Book *next;
  10. };
  11. void addlibrary(struct Book **);
  12. void getInput(struct Book *);
  13. void printflibrary(struct Book *);
  14. void freelibrary(struct Book *);

  15. void getInput(struct Book *book)
  16. {
  17.                 printf("输入书名:");
  18.                 scanf("%s", book->title);
  19.                 printf("\n输入作者:");
  20.                 scanf("%s", book->author);
  21. }


  22. void addlibrary(struct Book **library)
  23. {
  24.                 struct Book *book, *temp;

  25.                 book = (struct Book *) malloc(sizeof(struct Book));
  26.                 if(book == NULL)
  27.                 {
  28.                                 printf("内存分配失败!\n");
  29.                                 exit(1);
  30.                 }
  31.                 getInput(book);

  32.                 if(*library != NULL)
  33.                 {
  34.                                 temp = *library;
  35.                                 *library = book;
  36.                                 book->next = temp;
  37.                 }
  38.                 else
  39.                 {
  40.                                 *library = book;
  41.                                 book->next = NULL;
  42.                 }
  43. }

  44. void printflibrary(struct Book *library)
  45. {
  46.                 struct Book *book;
  47.                 book = library;

  48.                 while(book != NULL)
  49.                 {
  50.                                 printf("书名:%s\n", book->title);
  51.                                 printf("作者:%s\n", book->author);
  52.                                 book = book->next;
  53.                 }
  54. }

  55. void freelibrary(struct Book *library)
  56. {
  57.                 struct Book *book;

  58.                 while(library != NULL)
  59.                 {
  60.                                 book = library->next;
  61.                                 free(library);
  62.                                 library = book;
  63.                 }
  64. }

  65. int main()
  66. {
  67.                 struct Book *library = NULL;
  68.                 char ch;

  69.                 while(1)
  70.                 {
  71.                                 printf("是否输入数据");
  72.                                 do
  73.                                 {
  74.                                                 ch = getchar();
  75.                                 }while(ch != 'Y' && ch != 'N');

  76.                                 if(ch == 'Y')
  77.                                 {
  78.                                                 addlibrary(&library);
  79.                                 }
  80.                                 else
  81.                                 {
  82.                                                 break;
  83.                                 }
  84.                 }
  85.                 printflibrary(library);
  86.                 freelibrary(library);

  87.                 return 0;
  88. }
复制代码
最佳答案
2019-9-25 22:40:42
嗯,我也认为只有一个星号就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-25 22:40:42 | 显示全部楼层    本楼为最佳答案   
嗯,我也认为只有一个星号就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-26 08:37:22 | 显示全部楼层
人造人 发表于 2019-9-25 22:40
嗯,我也认为只有一个星号就可以了

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 08:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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