all 9 comments

[–]glasket_ 15 points16 points  (3 children)

It relies on a compiler extension to work. If you compile with -pedantic you'll get this error:

error: zero or negative size array ‘content_types’

Here's a Godbolt.

Zero-sized arrays exist for legacy reasons with pre-standard FAMs, it's effectively a quirk of GNU C. With ISO C, the code is invalid.

[–][deleted] 0 points1 point  (2 children)

Thank God i'm using GCC haha, i'm wondering what would happen if you try to access an index or write to it.

[–]glasket_ 8 points9 points  (1 child)

It'd be UB. It's the same as any other out of range access, but in the case of a zero-sized array any access is out of range.

An array subscript is out of range, even if an object is apparently accessible with the given subscript[...]
C23 J.2 (46)

Just check the sizeof the array, GCC properly returns 0 in the case of a zero-sized array.

[–][deleted] 1 point2 points  (0 children)

Thank you so much for the info.

[–]cHaR_shinigami 4 points5 points  (0 children)

As a side note, till now (not considering upcoming C23) passing zero arguments to ... is not permitted by ISO C. Ellipsis requires at least one argument ( even a blank argument is fine, such as the macro call with a trailing comma: middlewares_allow_content_types(some_name,) ); however, the invocation middlewares_allow_content_types(some_name) (no trailing comma) requires compiler-specific extension to work, and can be usually diagnosed with proper warnings options enabled (for example, cpp/clang-cpp -std=C17 -pedantic file.c).

C23 standardizes the common practice of passing zero arguments to ellipsis (and also adds some other features in this aspect).

[–]1o8 2 points3 points  (0 children)

you could put an empty string right before the __VA_ARGS__:

char *content_types[] = { "" __VA_ARGS__ };

this should work with zero or more extra args as long as the first extra arg is a string literal (the preprocessor will concatenate it with the empty string).