P99

◆ P99_LENGTH_ARR_ARG

#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 in 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.

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

See also
P99_VA_ARGS
P99_FSYMB

Definition at line 282 of file p99_args.h.

P99_FSYMB
#define P99_FSYMB(NAME)
Mangle NAME.
Definition: p99_args.h:223
i
P00_CLAUSE2 i(_Pragma("weak p00_getopt_comp"))(_Pragma("weak p00_getopt_comp