Search code examples
cincomplete-type

"parameter has incomplete type" warning


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?


Solution

  • As we've found out in the comments, the problem was that the definition of struct T occurred after the definition of T in the header. You really have things backwards here. The header should be defining all the types and function prototypes and your C files should be using them.

    What you want to be doing instead is change the signature of your insert function to receive a pointer to your data and the size of the data. Then you can allocate some memory for the data, copy it and store it. You don't need a specific type, just declare it a void *.

    void listInsertFirst(void *data, size_t data_size, int key, LinkedList* ListToInsertTo);
    

    Then the caller would do something like this:

    struct T { int foo; };
    struct T x = { ... };
    int someKey = ...;
    LinkedList *someList = ...;
    listInsertFirst(&x, sizeof x, someKey, someList);