一个c语言代码问题,求专业人士帮忙看一下代码错误的地方在哪
下面一段代码中好像是图书输出这个函数出了问题,一运行到那个地方就没有了,不知道怎么解决,求大佬帮忙解决一下
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct book)
void book_print(struct book *head_book);//图书输出
struct book *add_Book();//添加书籍
struct book
{
char book_name[30];
int book_num;
float price;
char author[20];
struct book *book_next;
};
int main()
{
struct book *head_book;
head_book = add_Book();
book_print(head_book);
return 0;
}
struct book *add_Book() //添加书籍,当图书编号为0时停止添加
{
struct book *head_book, *p1, *p2;
int n = 0;
p1 = p2 = (struct book*)malloc(LEN);
head_book = NULL;
printf("(添加图书)请输入书籍的名字:");
scanf("%s", p1->book_name);
printf("作者:");
scanf("%s", p1->author);
printf("编号:");
scanf("%d", &p1->book_num);
printf("单价:");
scanf("%f", &p1->price);
while (p1->book_num != 0)
{
if (p1->book_next == NULL)
{
head_book = p1;
}
else {
p2->book_next = p1;
}
p2 = p1;
p1 = (struct book*)malloc(LEN);
printf("(添加图书)请输入书籍的名字:");
scanf("%s", p1->book_name);
printf("作者:");
scanf("%s", p1->author);
printf("编号:");
scanf("%d", &p1->book_num);
printf("单价:");
scanf("%f", &p1->price);
}
p2->book_next = NULL;
return head_book;
}
void book_print(struct book *head_book) //图书输出
{
struct book *p;
p = head_book;
printf("本图书馆的所有书籍如下: ");
while (p!= NULL)
{
printf("书籍编号 名字 作者 单价 ");
printf("%d %s %s %.2f ", p->book_num, p->book_name, p->author, p->price);
p = p->book_next;
}
}
```
改好的部分程序
struct book *add_Book() //添加书籍,当图书编号为0时停止添加
{
struct book *head_book, *p1, *p2;
int n = 0;
head_book = p1 = p2 = (struct book*)malloc(LEN); // head_book就是第一个
// head_book = NULL;
printf("(添加图书)请输入书籍的名字:");
scanf("%s", p1->book_name);
printf("作者:");
scanf("%s", p1->author);
printf("编号:");
scanf("%d", &p1->book_num);
printf("单价:");
scanf("%f", &p1->price);
while (p1->book_num != 0)
{
// if (p1->book_next == NULL) //p1->book_next没有赋值,它是随机的
// {
// head_book = p1;
// }
// else
// {
p2->book_next = p1;
// }
p2 = p1;
。。。。。