3 ms·
I should note that if you just want to get the number of arguments for a call to a varargs function rather than trust people to terminate the arguments with NUL
by hlandau 11y ago
I should note that if you just want to get the number of arguments for a call to a varargs function rather than trust people to terminate the arguments with NULL properly, you can do this with the C99 preprocessor:
#define foo(...) foo(PP_NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
where PP_NUM_ARGS is a macro to determine the number of arguments; you can find such a macro easily by searching.
Of course you could just do #define foo(...) foo(__VA_ARGS__, NULL), but this lets you include NULL values in the list.
Since the count takes up the first argument to the actual varargs function, you can call it with zero arguments. No type checking, though as mentioned above you might be able to do something interesting about that with _Generic.
- ori_b 11y agoYou can also get this effect more simply by just adding a NULL in: #define foo(...) foo(__VA_ARGS__, NULL);