P99
#define P99_LENGTH_ARR_ARG (   T,
  ... 
)    ((size_t)P99_NARG(__VA_ARGS__)), (T[]){ __VA_ARGS__ }

Helper macro to declare a variable length parameter list.

Wrap your function into a macro that uses P99_LENGTH_ARR_ARG. If used through that macro, the correct value for number for the length of the array arr as in the following example will always be provided at compile time:

 unsigned P99_FSYMB(tutu)(unsigned a, size_t number, unsigned const*arr);
 #define tutu(A, ...) P99_FSYMB(tutu)(A, P99_LENGTH_ARR_ARG(unsigned const, __VA_ARGS__))

In the definition of the function you then may use an array of the arguments in the obvious way.

 unsigned P99_FSYMB(tutu)(unsigned a, size_t number, unsigned const*arr) {
   unsigned ret = 0;
   for (size_t i = 0; i < number; ++i) {
     ret += arr[i];
   }
   return ret % a;
 }

In this toy example tutu can be used as

 unsigned magic = tutu(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.

In the example number is the name of the `length' parameter that you want to use in the definition of the function.

This method here is generally more efficient than using P99_VA_ARGS since it results in code that can easier be inlined by the compiler. In particular, if a function as tutu above is called with all parameters being compile time constants, the call may completely be optimized away.

See also:
P99_VA_ARGS
P99_FSYMB

Definition at line 272 of file p99_args.h.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines