我有一个名为book的结构和链接到struct book的列表。 我必须将书籍添加到我的书籍列表中,然后才能显示列表。 我创建了一个函数“printBList”来在屏幕上打印我的列表信息。 但是当我试图打印book.id并且程序停止响应时,我做错了什么。 我相信这行“book * b = head-> book”和“printf(”book ID%d \ n“,b-> id);”是错误的,但我无法找到我必须写的话。 有人可以帮我吗?C中的列表中的结构 - 显示链接列表中存在的结构的成员
#include <stdio.h>
#include <stdlib.h>
#define MAXSTRING 100
#define MAXREVIEWS 100
typedef enum genres{
fiction,
sientific,
politics
};
typedef struct
{
char author[MAXSTRING];
char title[MAXSTRING];
enum genres genre;
int id;
char reviews[MAXREVIEWS][MAXSTRING];
} book;
typedef struct list
{
int BookID;
struct list * next;
struct book * book;
} BList;
void printBList(BList * head)
{
BList * current = head;
book *b=head->book;
while (current != NULL) {
printf("List ID:: %d\n", current->BookID);
printf("book ID%d\n", b->id);
//printf("%d\n", current->BookID);
current = current->next;
}
}
int main()
{
BList * head = NULL;
head = malloc(sizeof(BList));
if (head == NULL) {
return 1;
}
book b={"author 1","title 1",1,22,"review 1"};
head->next = NULL;
head = malloc(sizeof(BList));
head->BookID = 1;
head->next = malloc(sizeof(BList));
head->next->BookID = 24;
head->next->book;
head->next->next = NULL;
printBList(head);
return 0;
}
'的printf更 ( “书籍ID%d \ n” 个,B-> ID);' - >'的printf( “书籍ID%d \ n” 个,电流 - >书本 - > id);' – BLUEPIXY
修复如[this](http://ideone.com/WyzypY) – BLUEPIXY
'typedef enum genres {...};'是毫无意义的;它没有命名一个类型。一个好的编译器会对此提出警告。你可能打算'typedef枚举类型{...}流派;' - 这是有道理的。你应该确保你知道如何打开编译器的'最大'警告,并且你应该注意它给你的每一个警告。请记住,编译器知道更多关于C的知识 - 除非它认为代码中存在错误,否则它不会发出警告。 –