tags:

views:

2414

answers:

5

GCC 3.4.5 (MinGW version) produces a warning: parameter has incomplete type for line 2 of the following C code:

struct s;
typedef void (* func_t)(struct s _this);
struct s { func_t method; int dummy_member; };

Is there a way to fix this (or at least hide the warning) without changing the method argument's signature to (struct s *)?

Note: As to why something like this would be useful: I'm currently tinkering with an object-oriented framework; 'method' is an entry in a dispatch table and because of the particular design of the framework, it makes sense to pass '_this' by value and not by reference (as it is usually done)...

A: 

Hiding warnings is generally pretty easy - just look at the help for your particular compiler.

http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/index.html#//apple_ref/doc/uid/TP40001838

Note that suppressing warnings is generally not something I would advocate.

Tim
Easy, but not good practice.
Scottie T
Um, I know. But the person who asked the question asked about hiding the warning. I just pointed out how to do it. It seems a bit unfair for me to be downvoted for answering that part of his question...
Tim
Good edit. I revoke my downvote.
Scottie T
A: 

You want to call it with a function pointer. Why not use a void pointer instead?

typedef void (*func_t)(void*);

You MIGHT be able to pass a loosely-typed function pointer as well; I don't have a compiler on hand.

typedef void (*func_t)(void (*)());
HUAGHAGUAH
"You want to call it with a function pointer" Actually, I don't. struct s contains other members as well - I'll clarify the question in a minute...
Christoph
+1  A: 

You can't quite do this easily - according to the C99 standard, Section 6.7.5.3, paragraph 4:

After adjustment, the parameters in a parameter type list in a function declarator that is part of a definition of that function shall not have incomplete type.

Your options are, therefore, to have the function take a pointer to the structure, or to take a pointer to a function of a slightly different type, such as a function taking unspecified parameters:

typedef void (* func_t)(struct s*);  // Pointer to struct
typedef void (* func_t)(void *);     // Eww - this is inferior to above option in every way
typedef void (* func_t)();           // Unspecified parameters
Adam Rosenfield
But there is no definition of that function, is there...just declarations?
Jonathan Leffler
Thanks. Your last suggestion actually does what I want, but then I'll loose all type information (and get a warning when compiling with -Wstrict-prototypes) - so I'll most likely just stick with the version I got...
Christoph
Jonathan is correct. I added an answer detailing this...
Christoph
i think jonathan is right. it may be invalid in c (i don't know. i only know in C++ it would be valid), but the statement you shown us doesn't make it invalid. in his code. the function declarator is not part of a definition. it's merely declaring the function.
Johannes Schaub - litb
A: 

Switching to GCC 4 seems like it should work. MinGW version 4.3.0: http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=241304&release_id=596917

Sean
A: 

The warning seems to be a bug with the current MinGW version of gcc. Contrary to what Adam said, it is valid C99 - section 6.7.5.3, paragraph 12 explicitly allows this:

If the function declarator is not part of a definition of that function, parameters may have incomplete type and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types.

There seems to be no way to instruct (this version of) gcc to not print this warning - at least I could not find a switch which worked - so I'm just ignoring it for now.

Christoph