我有一个名为book的结构和一个链接到struct book的列表。 我必须将书籍添加到我的书籍列表中,然后显示列表。 我创建了一个函数“printBList”来在屏幕上打印我的列表信息。 但是当我试图打印book.id时,我做错了什么,程序停止响应。 我相信行“book * b = head-> book;”和“printf(”书ID%d \ n“,b-> id);”是错的,但我找不到我必须写的权利。 有人能帮助我吗?
#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;
}
答案 0 :(得分:5)
问题在于打印列表的方式,在这里:
while (current != NULL) {
printf("List ID:: %d\n", current->BookID);
printf("book ID%d\n", b->id); // <-- HERE
current = current->next;
}
您使用的是current
,但是您尝试打印已修复的b
。
你可能想说:
printf("book ID%d\n", current->book->id);
但还有更多问题:
此外,你打算写:
typedef struct book {
..
} book;
在为next
创建空间之前,您正在访问head
head
,此处:
head->next = NULL;
head = malloc(sizeof(BList));
所以改成它:
head = malloc(sizeof(BList));
head->next = NULL;
此外,这一行:
head->next->book;
什么也没做。
接下来,您应该检查警告:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:7:1: warning: typedef requires a name [-Wmissing-declarations]
typedef enum genres{
^~~~~~~
main.c:34:11: warning: incompatible pointer types initializing 'book *' with an
expression of type 'struct book *' [-Wincompatible-pointer-types]
book *b=head->book;
^ ~~~~~~~~~~
main.c:53:39: warning: suggest braces around initialization of subobject
[-Wmissing-braces]
book b={"author 1","title 1",1,22,"review 1"};
^~~~~~~~~~
{ }
main.c:61:17: warning: expression result unused [-Wunused-value]
head->next->book;
~~~~~~~~~~ ^~~~
main.c:53:10: warning: unused variable 'b' [-Wunused-variable]
book b={"author 1","title 1",1,22,"review 1"};
^
5 warnings generated.
当你输入dede的东西时,你需要给出一个同义词,所以为你的枚举做这个:
typedef enum genres{
..
} genres;
由于reviews
是一个2D数组,您可以这样做:
book b={"author 1","title 1",1,22, {"review 1"} };
此外,请不要使用幻数,在这种情况下为1,因为您有enum
用于此目的。
然后,我更改了您的main()
,将同一本书添加两次,使用不同的列表ID。把所有东西放在一起,我们得到:
#include <stdio.h>
#include <stdlib.h>
#define MAXSTRING 100
#define MAXREVIEWS 100
typedef enum genres{
fiction,
sientific,
politics
} genres;
typedef struct book
{
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;
while (current != NULL) {
printf("List ID:: %d\n", current->BookID);
printf("book ID%d\n", current->book->id);
current = current->next;
}
}
int main()
{
BList * head = NULL;
head = malloc(sizeof(BList));
if (head == NULL) {
return 1;
}
book b={"author 1","title 1", sientific ,22,{"review 1"}};
head->next = NULL;
head->BookID = 1;
head->book = &b;
head->next = malloc(sizeof(BList));
head->next->BookID = 24;
head->next->book = &b;
head->next->next = NULL;
printBList(head);
return 0;
}
输出:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
List ID:: 1
book ID22
List ID:: 24
book ID22
PS:
free()
。malloc()
是否成功,这是一个很好的做法
检查其返回值是否为NULL
(表示失败)。阅读更多
在
How detect malloc failure?