【发布时间】:2021-02-21 11:16:27
【问题描述】:
所以,我有这 2 个结构和一个全局变量
struct Tasks{
int tid;
int difficulty;
struct Tasks *next;
};
struct Head_GL{
int tasks_count[3];
struct Tasks *head;
};
struct Head_GL *tasks_head;
我必须创建一个按难度加入顺序的链接列表。如何进行比较和阅读难度。我这样做了tasks_head->head->difficulty 并给了我分段错误
【问题讨论】:
-
您需要先为它们分配内存。
-
tasks_head->head=(struct Tasks*)malloc(sizeof(struct Tasks));像这样?
-
你还需要分配
tasks_head。 -
所以在分配 tasks_head->head 之前我必须分配 tasks_head tasks_head=(struct Head_GL*)malloc(sizeof(struct Head_GL));
-
你可以放弃演员表,他们没用:
(struct Tasks*)malloc(sizeof(struct Tasks))->malloc(sizeof(struct Tasks))
标签: c pointers data-structures linked-list