3

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)...

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Which version of GCC? That exact snippet of code (plus a main function) gives me no warnings with the GCC included with Ubuntu, which is 4.3.2. Is there something else involved that is causing the warning? – Sean Dec 16 '08 at 15:44
  • I'm currently stuck with the MinGW version of gcc, which is 3.4.5(?). But nice to know that the problem might eventually just go away... – Christoph Dec 16 '08 at 15:51
  • GCC 3.4.6 complains under -Wall; GCC 4.3.2 does not - Solaris 10. – Jonathan Leffler Dec 16 '08 at 16:34
  • How would you ever use this structure/function? Is this a maximally reduced example (generally a good thing - though you might have gone one step to far in the minimization), or is it a fabricated test case? I can't see how I'd get a useful result out of a structure or function like this. – Jonathan Leffler Dec 16 '08 at 16:37
  • Why does it make sense to pass by value? – Sean Dec 17 '08 at 04:05

5 Answers5

1

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
  • 390,455
  • 97
  • 512
  • 589
  • But there is no definition of that function, is there...just declarations? – Jonathan Leffler Dec 16 '08 at 17:21
  • 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 Dec 16 '08 at 17:23
  • 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 Dec 28 '08 at 05:24
1

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
  • 2,632
  • 2
  • 27
  • 35
0

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
  • 20,184
  • 24
  • 117
  • 214
  • 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 Dec 16 '08 at 15:28
0

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
  • 164,997
  • 36
  • 182
  • 240
-1

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
  • 1,836
  • 10
  • 7
  • "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 Dec 16 '08 at 16:47