I'm reading the code of wiredtiger. I see a function definition as
WT_CURSOR::set_key(WT_CURSOR * cursor, ...)
what does the '...' means here? how can the compiler compile such code?
Thanks
I'm reading the code of wiredtiger. I see a function definition as
WT_CURSOR::set_key(WT_CURSOR * cursor, ...)
what does the '...' means here? how can the compiler compile such code?
Thanks
It means that the function accepts a variable number of arguments after the named arguments (possibly zero). The function would use a va_list
and the associated functions (va_start
, va_arg
, and va_end
) to process the arguments.
An example:
#include <stdio.h>
#include <stdarg.h>
// n: number of doubles
// ... list of doubles
double average(int n, ...)
{
double accum = 0.0;
int i;
va_list vl;
va_start(vl, n);
for(i = 0; i < n; i++)
{
accum += va_arg(vl, double);
}
va_end(vl);
return accum/(double)n;
}
int main()
{
double avg = average(5, 1.0, 1.0, 6.5, 3.3, -5.8);
printf("%f\n", avg);
return 0;
}
Output: 1.200000