yotamoo
yotamoo

Reputation: 5452

"error: dereferencing pointer to incomplete type" in c

Ok, I've seen this question on google a million times, but still no success for me. I am trying to create a linked list. Here's the struct defined in List.c:

typedef struct List
{
    unsigned int size;
    ObjectP head;
} List;

Now as you'll notice both the struct and the typedef are named LIST, which is fine as far as I know. But the important thing is that the struct is NOT anonymous.

Now in List.h (which of course is included in List.c)I've defined another typedef:

typedef struct List* ListP;

Which as far as I understand it defines ListP to be a pointer to a struct called List, just like I really named it.

And here's where I get the errors:

void freeList(ListP list)
{
    ObjectP obj1;
    ObjectP obj2;
    if (list != NULL)
    {
        obj1 = list->head;
        while (obj1->next != NULL)   <---- HERE
        {
            obj2 = obj1;
            obj1 = obj1->next;   <---- HERE
        freeObject(obj2);
        }
    free(list);
    }
}

I don't understand why obj1 = list->head; is ok, but everywhere else I try to gain access to struct members I have this error. As I said I've tried looking for it, but other ppl had mistakes I can't find in my code. Thanks!

EDIT: List.c also includes Object.h so this is not the problem. The definition of object as is follow:

in Object.h I have typedef struct Object* ObjectP; and in Object.c I have

typedef struct Object
{
    void* key;
    ObjectP next;
} Object;

Still don't know what the problem is!

Upvotes: 1

Views: 2679

Answers (2)

brianmearns
brianmearns

Reputation: 9967

It looks like you define the struct for ObjectP in a different C file (probably Object.c). When the compiler is compiling List.c, it doesn't know anything defined in other c files, it only knows what's been defined in h files that have been included in the current c file.

Structs that are shared by different c files have to be defined in a header file that can be included in all of them. So if you just move the definition for struct Object to Object.h, you should be ok.

Upvotes: 2

deStrangis
deStrangis

Reputation: 1930

ObjectP is defined as a pointer to Object and Object has a member of type ObjectP. This is a circular dependency.

Try:

typedef struct object_st {
    void* key;
    struct object_st *next;
} Object;

typedef ObjectP *Object;

Oh, and cast for assignment:

obj1 = (ObjectP) obj1->next;

Upvotes: 0

Related Questions