I have a struct named book and a list linked to struct book. I must add books to my book list and then to display the list. I created a function "printBList" to print at screen the information of my list. But i do something wrong when I am trying to print the book.id and the program stops to responding. I believe that the lines "book *b=head->book;" and "printf("book ID%d\n", b->id);" are wrong, but I can not find how I have to write then right. Could someone help me?
#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;
}