简体   繁体   中英

Inserting Linked List Node into ascending sort singly linked List GCC error: dereferencing pointer to incomplete type

I'm trying to insert a node into the proper place (in order) in a linked list that is sorted in ascending order. I keep getting the GCC error "Error: dereferencing pointer to incomplete type". I've been working off of the code in this stackoverflow post . Below is my code:

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

I'm trying to return a pointer to the front of the list so other linked list manipulation can happen once returned. I've been doing some research on StackOverflow and it sounds like it's referring to the struct sNode that is causing the problem. I've seen different posts on using typedef to declare the struct. So I've tried it with and without using typedef to declare it. I've also seen posts suggesting #including & but that didn't work either. I'm using GCC 4.6.3 Any help is appreciated thanks!

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

You must typedef the struct to some name,

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

for example. Without the name you typedef it to, it's invalid anyway, and the type still must be referred to with the struct keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM