"parameter has incomplete type" warning

Belgi picture Belgi · May 13, 2011 · Viewed 13.2k times · Source

I have this in a C file:

struct T
{
    int foo;
};

the C file has an include to an h file with those lines:

typedef struct T T;
void listInsertFirst(T data, int key, LinkedList* ListToInsertTo);

the function listInsertFirst is the one I'm getting the warning on. How can I fix it?

Answer

mu is too short picture mu is too short · May 13, 2011

When you include the header file, the compiler knows that T is a structure of unknown size and that listInsertFirst wants one as its first argument. But the compiler cannot arrange a call to listInsertFirst as it doesn't know how many bytes to push on the stack for the T data parameter, the size of T is only known inside the file where listInsertFirst is defined.

The best solution would be to change listInsertFirst to take a T* as its first argument so your header file would say this:

extern void listInsertFirst(T *data, int key, LinkedList* ListToInsertTo);

Then you get an opaque pointer for your T data type and, since all pointers are the same size (in the modern world at least), the compiler will know how to build the stack when calling listInsertFirst.