C typedef: parameter has incomplete type

2020-07-23 02:52发布

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

标签: c gcc
5条回答
相关推荐>>
2楼-- · 2020-07-23 03:30

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 (*)());
查看更多
女痞
3楼-- · 2020-07-23 03:37

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.

查看更多
相关推荐>>
4楼-- · 2020-07-23 03:42

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.

查看更多
三岁会撩人
5楼-- · 2020-07-23 03:43

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

查看更多
SAY GOODBYE
6楼-- · 2020-07-23 03:46

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
查看更多
登录 后发表回答