课上代码运行只能运行一半
本帖最后由 孤世星辰 于 2021-5-10 10:42 编辑#include <stdio.h>
#include <stdlib.h>
struct Book
{
char title;
char author;
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
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)
{
temp=*library;
*library=book;
book->next=temp;
}
else
{
*library=book;
book->next=NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count=1;
book=library;
while(book!=NULL)
{
printf("Book%d:",count);
printf("书名%s:",book->title);
printf("作者%s:",book->author);
book=book->next;
count++;
}
}
void releaseLibrary(struct Book *library)
{
while (library!=NULL)
{
free(library);
library=library->next;
}
}
main(void)
{
struct Book *library=NULL;
int ch;
while(1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch=getchar();
}while(ch!='Y'&&ch!='N');
if(ch=='Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("请问是否需要打印书籍信息(Y/N):");
do
{
ch=getchar();
}while(ch!='Y'&&ch!='N');
if(ch=='Y')
{
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
if(book=NULL) 人造人 发表于 2021-5-10 13:02
if(book=NULL)
谢谢,一般这种错误怎么找到呢,他也没有提示,我百度也是说越界了 孤世星辰 发表于 2021-5-10 14:47
谢谢,一般这种错误怎么找到呢,他也没有提示,我百度也是说越界了
$ gcc -g -Wall -o main main.c
main.c: In function ‘addBook’:
main.c:22:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
22 | if(book=NULL)
| ^~~~
main.c: At top level:
main.c:64:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
64 | main(void)
| ^~~~
人造人 发表于 2021-5-10 16:14
谢谢
页:
[1]