https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112761
Bug ID: 112761
Summary: Using incomplete array types in function prototypes
doesn't work
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: ville.syrjala at linux dot intel.com
Target Milestone: ---
struct foo;
void func(struct foo bar[]);
int main(void)
{
}
error: array type has incomplete element type ‘struct foo’
3 | void func(struct foo bar[]);
| ^~~
This prevents using the array syntax in function prototypes with opaque types.
So I have to give up either:
- the array syntax, which means there is no longer any hint to the reader
whether the function takes an array or a pointer to a single object
- or the opaque type which is even worse since the clean abstraction is ruined
(not to mention the resulting stable ABI issues).
I couldn't immediately find anything in the spec that says this is illegal:
- "6.7.6.2 Array declarators ...
If the size is not present, the array type is an incomplete type."
- "6.7.6.3 Function declarators ...
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."
But maybe I just don't know how to read the spec correctly?
Side note:
This also affects the (arguably hideous) "ptr[static 1]" syntax which would be
beneficial to indicate that you can't pass in a null pointer. But I suppose
that one is explicitly forbidden by the spec since we do specify a size for the
"array" and thus it is not allowed to be an incomplete type.