P99
#define P99_VA_ARGS (   X)    size_t X /*!< the number of arguments that follow */, ...

Helper macro to declare a variable length parameter list.

Inside the declared function X will of size_t and should hold the actual length of the list. It can be used as the argument to va_start.

Wrap your function into a macro that uses P99_LENGTH_VA_ARG. If used through that macro, the correct value for X will always be provided at compile time. Declare such a function as this:

 unsigned P99_FSYMB(toto)(unsigned a, P99_VA_ARGS(number));
 #define toto(A, ...) P99_FSYMB(toto)(A, P99_LENGTH_VA_ARG(__VA_ARGS__))

In the definition of the function you then may use the va_start etc from stdarg.h to tread the argument list.

 unsigned P99_FSYMB(toto)(unsigned a, P99_VA_ARGS(number)) {
     unsigned ret = 0;
     va_list ap;
     va_start(ap, number);
     for (size_t i = 0; i < number; ++i) {
       ret += va_arg(ap, unsigned);
     }
     va_end(ap);
     return ret % a;
 }

In this toy example toto can be used as

 unsigned magic = toto(3, 1, 3, 5, 7);

which will result in converting 1, 3, 5, 7 (the variable arguments) to unsigned, computing their sum, i.e 16u, and compute that value mod 3u (the fixed argument a). So magic should hold the value 1u thereafter. (But beware of implicit integer promotion rules for integers of small width.)

Parameters:
Xis the name of the `length' parameter that you want to use in the definition of the function. As in the example above it should be then used as the second argument to va_start and as a loop boudary when you actual handle the argument list. X is implicitly declared to have type size_t.
See also:
P99_LENGTH_ARR_ARG for a way that is generally more efficient than using va_list
P99_LENGTH_VA_ARG
P99_FSYMB

Definition at line 203 of file p99_args.h.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines