在结构指针的next *字段中访问int

问题描述

我有一个指向Tasks结构的指针,该结构具有三个字段tid,difficulty和next。我想将一个任务中的下一个任务的难度与一个数字进行比较,但是当我尝试以下操作时,我得到了附件错误

struct Tasks
{
    int tid;                      /*Task's identifier*/
    int difficulty;               /*Task's difficulty*/
    struct Tasks *next;           /*Pointer to the next node*/  
};

struct Tasks* curr = (*player)->tasks_head->next;
if(curr->next->difficulty < difficulty) {...}

这是我为新节点分配内存的方式:

struct Tasks* node = (struct Tasks*) malloc(sizeof(struct Tasks)); // Could return null if not enough mem
    
if(node == NULL) return 0;
// add data to the new node
node->tid = tid;
node->difficulty = difficulty;
node->next = NULL;

抛出异常:读取访问冲突。 curr-> next为0xCDCDCDCD

我以为我可以通过执行*(curr->next)->difficulty来解除对下一个指针的引用,但这不允许我进行编译,我也不明白为什么。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)