0

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!

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help but I'm still getting the same error as before. I changed the struct definition as you suggested.
I suspect struct studentNode is not declared before it is used in addStudentNode. (Probably, it should be struct sNode anyway.) After your edit, do you still get the dereferencing pointer to incomplete type error? If so, on which line?
I cleaned up the code sorry about that. I'm still getting the "dereferencing pointer to incomplete type error" and it points to the line "while(headPtr->next != NULL && headPtr->next->sid < pSid){" when I change that line to just "while(headPtr->next != NULL){" it compiles (but won't do what it's supposed to do). So I think specifically it's the "headPtr->next->sid < pSid" part. The struct sNode declaration is before this function code. Thanks again.
Have you compiled after inserting the typedef-name? The code you have in the question now should compile. (And compiles without any problems with gcc -c -std=c99 -Wall -Wextra -pedantic -pedantic-errors.)
Yup I compiled it again and it worked. Not sure why it didn't the first time. Just one of those things I guess. Thanks again for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.