代码
#include<stdio.h>
#include<stdlib.h>
struct Book
{
char title[128];
char author[40];
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入数据:");
scanf("%s",book->title);
scanf("%s",book->author);
}
void addbook(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)
{
*library=book;
book->next=NULL;
}
else
{
temp=*library;
*library=book;
book->next=NULL;
}
}
int main()
{
struct Book *library;
addbook(&library);
return 0;
}
你只是定义了他,并没有给他初始化,而且library是指针的指针,你必须对每一层指针都复制,你可以用malloc赋值或者指向别的变量,否则只是定义的话就是NULL