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

标签 c gcc struct linked-list

我试图将一个节点插入到按升序排序的链接列表中的正确位置(按顺序)。我不断收到 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/

相关文章:

c - valgrind 在这段代码上出现无效写入错误

c - 整数 a 和 *(&a) 有什么区别?

c++ - 在构造函数中分配数组时出错

c - 从不兼容的指针类型返回(const vs non-const)。 C/海湾合作委员会

c - 从 gcc 获取混合 C 和内联汇编

转换为非标量类型

c - 如何在 C 中将客户端程序重定向到新的任意端口

c - Eclipse 符号无法解析

c - C语言中如何分配不同类型的内存?

c - C使用构造函数创建结构数组