gpt4 book ai didi

c - C 中的结构内部列表 - 显示链接列表中存在的结构的成员

转载 作者:行者123 更新时间:2023-12-01 14:41:30 25 4
gpt4 key购买 nike

我有一个名为 book 的结构和一个链接到结构 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;
}

最佳答案

问题出在打印列表的方式上,在这里:

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;

在为 head 创建空间之前,您正在访问 headnext,此处:

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.

当您键入某些内容时,您需要给出一个同义词,因此请为您的枚举执行此操作:

typedef  enum genres{
..
} genres;

由于 reviews 是一个二维数组,因此您可以这样做:

book b={"author 1","title 1",1,22, {"review 1"} }; 

此外,请勿使用魔数(Magic Number),例如本例中的 1,因为您有一个用于此目的的枚举


然后,我对您的 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:

  1. 不要忘记取消分配动态分配的空间。做这与 free() .
  2. 检查 malloc() 是否成功是一个很好的做法,方法是检查其返回值是否为NULL(表示失败)。阅读更多在 How detect malloc failure?

关于c - C 中的结构内部列表 - 显示链接列表中存在的结构的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com