gpt4 book ai didi

c - 将链表节点插入升序单链表GCC错误: dereferencing pointer to incomplete type

转载 作者:行者123 更新时间:2023-11-30 14:23:42 25 4
gpt4 key购买 nike

我试图将一个节点插入到按升序排序的链接列表中的正确位置(按顺序)。我不断收到 GCC 错误“错误:取消引用指向不完整类型的指针”。我一直在研究这个 stackoverflow post 中的代码。下面是我的代码:

typedef struct sNode  {         
int sid;
struct sNode *next;
}sNode;

sNode* addsNode (struct sNode *headPtr, int pSid)
{
struct sNode *ptr, *root = headPtr;
ptr = malloc (sizeof(struct sNode));

if(headPtr == NULL){ //In other code I've already check for a NULL list, pointer
ptr->sid = pSid;
}
else{
while(headPtr->next != NULL && headPtr->next->sid < pSid){
//while(headPtr->next != NULL){ --> Compiles when uncommented

headPtr = headPtr->next;
}//while
ptr->sid = pSid;

}//else

return root;
}//addsNode

我试图返回一个指向列表前面的指针,以便在返回后可以进行其他链接列表操作。我一直在对 StackOverflow 进行一些研究,听起来它是指导致问题的 struct sNode 。我看过关于使用 typedef 声明结构的不同帖子。所以我尝试过使用和不使用 typedef 来声明它。我也看到过建议 #include & 的帖子,但这也不起作用。我正在使用 GCC 4.6.3 感谢任何帮助!

最佳答案

typedef struct sNode  {         
int sid;
struct sNode *next;
};

您必须将结构typedef指定为某个名称,

typedef struct sNode  {         
int sid;
struct sNode *next;
} sNode;
例如。如果没有您输入的名称,它无论如何都是无效的,并且仍然必须使用 struct 关键字引用该类型。

关于c - 将链表节点插入升序单链表GCC错误: dereferencing pointer to incomplete type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12627387/

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