|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 落日飞花 于 2021-7-26 21:50 编辑
出错语句
scanf("%[^\n]",(*book).title);
定义:
struct Book *book = NULL;
book = (struct Book *)malloc(sizeof(struct Book));
结构体定义
struct Book
{
char title[128];
char author[40];
struct Book * next;
}
完整代码
#include <stdio.h>
struct Book
{
char title[128];
char author[40];
struct Book * next;
};
void addBook(struct Book **lib);//添加新书
void getInput(struct Book *book);//从缓存区读入新书内容
void printlib(struct Book *);//打印图书
void printlib(struct Book* book)
{
printf("Book Name: %s\n",book->title);
printf("Book Author: %s\n",book->author);
}
void getInput(struct Book *book)
{
printf("Please enter a new Book!\n");
printf("Please enter book's name:");
scanf("%[^\n]",(*book).title);// 此处出现问题
//gets(book->title);
printf("Please enter the anthor:");
scanf("%[^\n]",book->author);
}
void addBook(struct Book **lib)
{
struct Book *book = NULL;
book = (struct Book *)malloc(sizeof(struct Book));//分配栈区内存
if (book = NULL)
{
printf("new book create error");
exit(1);
}
getInput(book);
book->next = *lib;
*lib = book;
}
int main(void)
{
struct Book *library = NULL;
addBook(&library);
printlib(library);
while(1);
return 0;
}
|
|