我有一個名爲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的知識 - 除非它認爲代碼中存在錯誤,否則它不會發出警告。 –