Warning "parameter has an invalid type"

I have this in the C file:

struct T { int foo; }; 

The C file has an include for the h file with these lines:

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

The listInsertFirst function is the one to which I receive a warning. How can i fix this?

+6
source share
5 answers

As we found out in the comments, the problem was that the definition of struct T happened after the definition of T in the header. You really have things back. The header should define all types and prototypes of functions, and your C files should use them.

Instead, you want to change the signature of the insert function to get a pointer to your data and data size. Then you can allocate some memory for the data, copy it and save. You don't need a specific type, just declare it void * .

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

Then the caller will do something like this:

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

When you include the header file, the compiler knows that T is a structure of unknown size and that listInsertFirst wants it to be the first argument. But the compiler cannot organize a call to listInsertFirst , because it does not know how many bytes to push on the stack for the T data parameter, the size of T is known only inside the file where listInsertFirst defined.

The best solution would be to change listInsertFirst to take T* as its first argument, that your header file, said the following:

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

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

+2
source

Are you sure this is the first parameter that is the problem? Of course, try temporarily changing the type of the parameter from T to int . Most likely, the third parameter is actually a problem.

Many compilers do not point out a problem in these matters very well.

0
source

Try moving the structure definition to the h file before typedef.

0
source
  • Define struct T in the header, not in the .c file;
  • Choose different names for the structure and typedef.
0
source

Source: https://habr.com/ru/post/888066/


All Articles