|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第一个代码:
void getinput(struct Book* book)
{
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
printf("请输入书名;\n");
scanf("%s",book->title);
printf("请输入作者;\n");
scanf("%s",book->author);
}
void addbook(struct Book**head)
{
struct Book* temp;
struct Book* book;
getinput(book);
if(*head == NULL)
{
*head = book;
book->next = NULL;
}
else
{
temp = *head;
*head = book;
book->next = temp;
}
}
第二个代码:
void getinput(struct Book* book)
{
if (book == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
printf("请输入书名;\n");
scanf("%s",book->title);
printf("请输入作者;\n");
scanf("%s",book->author);
}
void addbook(struct Book**head)
{
struct Book* temp;
struct Book* book;
book = (struct Book*)malloc(sizeof(struct Book));
getinput(book);
if(*head == NULL)
{
*head = book;
book->next = NULL;
}
else
{
temp = *head;
*head = book;
book->next = temp;
}
}
代码是小甲鱼上课的例子,区别在于book = (struct Book*)malloc(sizeof(struct Book));这句话在哪个函数里面,但是一个能跑一个不能跑是为什么呀 |
|