Thread: structures: dereferencing pointer to incomplete type

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    structures: dereferencing pointer to incomplete type

    I'm trying to make this basic 'MUD' style game to practice coding, the problem I was having was trying to have two structure types, each of which can contain a pointer to the other type. After I finally got that to compile via the forward typedef declarations, I can no longer access the structure elements and instead get the error: dereferencing pointer to incomplete type


    Code:
    typedef struct node Items;
    typedef struct node Areas;
    
    struct Areas{
    char desc[255];
    char name[255];
    Items*items;
    int n,s,w,e,u,d;
    }; 
    
    extern struct Areas a[1000];
    
    struct Items{
    char*desc;
    char*name;
    Areas*loc;
    Items*next;
    Items*prev;
    };
    Here is the code giving me the errors:

    Code:
    	Items*new=createitem("NULL", "NULL", currentarea);
    			name=strtok(input,",");
    			new->name=name;
    			desc=strtok(NULL, " ");		
    			new->desc=desc;
    			if (a[currentarea].items!=NULL){
                                   new->next=a[currentarea].items; 
                                    a[currentarea].items->prev=new; 
                                    a[currentarea].items=new;}
    I thought this might fix it, but no luck:
    Code:
    typedef struct items Items;
    typedef struct areas Areas;
    -Adam Rinkleff
    Last edited by Adam Rinkleff; 07-26-2011 at 01:17 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Without a typedef use the entire struct tag, that is
    Code:
    struct Areas
    {
        char desc[255];
        char name[255];
        struct Items*items;
        int n,s,w,e,u,d;
    };
    Last edited by itCbitC; 07-26-2011 at 01:53 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Where/what is "struct node" defined as?

    Tim S.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Some suggested reading that might answer your question: C-FAQ 1.15
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by stahta01 View Post
    Where/what is "struct node" defined as?

    Tim S.
    I don't know, I thought I have defined these structs. The error seems to be that it isn't properly defined. I tried what itCbitC said, but the following still gives the same errors:

    Code:
    typedef struct node Items;
    typedef struct node Areas;
    
    struct Areas{
    char desc[255];
    char name[255];
    struct Items*items;
    int n,s,w,e,u,d;
    }; 
    
    
    struct Items{
    char*desc;
    char*name;
    struct Areas*loc;
    struct Items*next;
    struct Items*prev;
    };
    
    
    
    
    extern struct Areas a[1000];

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Code:
    struct Areas{
    char desc[255];
    char name[255];
    struct Items*items;
    int n,s,w,e,u,d;
    }; 
    
    
    struct Items{
    char*desc;
    char*name;
    struct Areas*loc;
    struct Items*next;
    struct Items*prev;
    };
    
    typedef struct Items Items;
    typedef struct Areas Areas;
    Perhaps this is correct as per Andrew Hunter's link, at least it seems to compile.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Because the typedefs need to be fixed otherwise the highlighted line's incomplete:
    Code:
    typedef struct node Items;  /*  needs to be:   typedef struct Items Items  */
    typedef struct node Areas;  /*  needs to be:   typedef struct Areas Areas  */
    
    struct Areas{
    char desc[255];
    char name[255];
    struct Items *items;   /* creates an incomplete type unless typedefs are fixed */
    int n,s,w,e,u,d;
    }; 
    
    
    struct Items{
    char*desc;
    char*name;
    struct Areas*loc;
    struct Items*next;
    struct Items*prev;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another 'dereferencing pointer to incomplete type'
    By thealmightyone in forum C Programming
    Replies: 7
    Last Post: 11-25-2010, 08:49 PM
  2. dereferencing pointer to incomplete type
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 07-28-2010, 09:32 AM
  3. dereferencing pointer to incomplete type
    By johnny_ in forum C Programming
    Replies: 4
    Last Post: 03-01-2010, 10:46 AM
  4. error: dereferencing pointer to incomplete type
    By surlyTomato in forum C Programming
    Replies: 10
    Last Post: 08-22-2009, 08:04 AM
  5. dereferencing pointer to incomplete type
    By kataya in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 01:37 AM