r/cprogramming • u/spherical_shell • Jul 14 '23
What's the rationale behind "empty structs and zero-length arrays are (apparently) non-standard"?
Many posts on the internet like https://stackoverflow.com/a/9723093 and https://stackoverflow.com/a/1626493 say that zero-length arrays and structs with no members are actually non-standard.
This is quite surprising. Does anyone know why this is the case? Are there any problems of making them standard? Are there any caveats (in terms of implementing the compiler)?
11
Upvotes
4
u/ellisonch Jul 15 '23 edited Jul 15 '23
The C99 rationale gives a small bit of... well, rationale that you might find helpful.
[and yes, all three spelling mistakes are in the original]
If you haven't seen them, flexible array members (arrays with no size at the end of a struct) are allowed since C99, and accomplish most of the things you generally want "empty" arrays for.
5
u/zhivago Jul 14 '23
C does not support any zero length objects, which is an important consideration for pointer arithmetic.
Consider what might happen if it did.
Should
p + 1 == p?1
u/[deleted] Jul 14 '23
There is no definite answer to this that will satisfy everybody. For every reason X you can give of why it is like that, there is always a reason Y of why it isn't made like that to resolve this issue. I can only guess here:
1
u/McUsrII Jul 15 '23
I just humbly wonder:
What is the use of a zero length array or an empty struct?
What are the advantages of having those constructs available?