马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个程序我在下面freelibrary(struct Book *library)函数声明的时候用了一个*library没又用两个**号一个型号在我的理解中不也是调用了main函数中的library指针并对他进行改变么?并且我要释放的是这个指针占用的空间并不是更改里边的数据我感觉一个*号应该够了的,但是在小甲鱼的视频中用的是两个**我想知道为什么,并且我在这里用一个型号也是能释放我所申请的内存地址的编译器也没有报错,那位大神能给讲解一下谢谢#include<stdio.h>
#include<stdlib.h>
#define MAX1 128
#define MAX2 40
struct Book
{
char title[MAX1];
char author[MAX2];
struct Book *next;
};
void addlibrary(struct Book **);
void getInput(struct Book *);
void printflibrary(struct Book *);
void freelibrary(struct Book *);
void getInput(struct Book *book)
{
printf("输入书名:");
scanf("%s", book->title);
printf("\n输入作者:");
scanf("%s", book->author);
}
void addlibrary(struct Book **library)
{
struct Book *book, *temp;
book = (struct Book *) malloc(sizeof(struct Book));
if(book == NULL)
{
printf("内存分配失败!\n");
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;
book = library;
while(book != NULL)
{
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
book = book->next;
}
}
void freelibrary(struct Book *library)
{
struct Book *book;
while(library != NULL)
{
book = library->next;
free(library);
library = book;
}
}
int main()
{
struct Book *library = NULL;
char ch;
while(1)
{
printf("是否输入数据");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
addlibrary(&library);
}
else
{
break;
}
}
printflibrary(library);
freelibrary(library);
return 0;
}
|