求大神指教一下,这是哪里的问题,内存分配的问题吗?(我看着小甲鱼的视频打出来....
#include<stdio.h>#include<stdlib.h>
void getInput(struct Book *book);
void addBook(struct Book **library);
void printfLibrary(struct Book *library);
void releaseLibrary(struct Book **library);
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("内存分配失败!");
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;
int count=1;
while(book!=NULL)
{
printf("Book%d:",count);
printf("书名:%s",book->title);
printf("作者:%s",book->author);
book=book->next;
count++;
}
}
void releaseLibrary(struct Book **library)
{
struct Book *temp;
while(library!=NULL)
{
temp=*library;
*library=(*library)->next;
free(temp);
}
}
int mian(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')
{
printfLibrary(library);
}
releaseLibrary(&library);
addBook(&library);
return(0);
}
I found 4 problems when watching your code
P 1. Undefined reference to `main'
A 1. Your entry point typed in int mian(void), instead of int main(void)
P2. struct Book{} declared under function()
A 2. When declared' function try to find struct Book that does not declared before, error pops up.
P 3. In printfLibrary() section, book address was empty
A 3. You have to set book = library;
P 4. Write argument name when declare function e.g void addBook(struct Book **xyz);
A 4. When you use the function to print your struct, it may not show the input. whould be write as void addBook(struct Book **);
Code should be like this:
struct Book
{
char title;
char author;
struct Book *next;
};
void addBook(struct Book **);
void getInput(struct Book *);
void printfLibrary(struct Book *);
void releaseLibrary(struct Book **);
_Bool pCOMMAND(void);
_Bool pDEBUG(void);
_Bool pEND(void);
_Bool debugSwitch = FALSE;
int main(void)
{
struct Book *library=NULL;
char 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')
{
printfLibrary(library);
}
releaseLibrary(&library);
return 0;
}
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
printf("%s\n",book->author);
return;
}
void addBook(struct Book **library)
{
struct Book *book;
struct Book *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL)
{
printf("内存分配失败!");
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 = null;
int count = 1;
book = library;
while (book != NULL)
{
printf("书名:%s\n",(*book).title);
printf("作者:%s\n",(*book).author);
book=book->next;
count++;
}
}
void releaseLibrary(struct Book **library)
{
struct Book *temp;
while(library!=NULL)
{
temp=*library;
*library=(*library)->next;
free(temp);
}
}
页:
[1]