1

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

zhihuifan
  • 1,093
  • 2
  • 16
  • 30

1 Answers1

6

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

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Thanks, I just read this [artical](https://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/), I am still confused with the first parameter. in the wiredtiger case, ``` WT_CURSOR *cursor; session->create(session, "table:kvtab", "key_format=S,value_format=S"); session->open_cursor(session, "table:kvtab", NULL, NULL, &cursor); cursor->set_key(cursor, "key1");``` what does the first parameter means here? – zhihuifan Jun 04 '17 at 13:47
  • @zhihuifan which one is the first parameters? Like I said above, the number of arguments depends on the number of columns in the key part of the table (and similarly with set_value in value part of the table). So it depends on this call `session->create(session, "table:kvtab", "key_format=S,value_format=S")` – amirouche Jun 25 '17 at 20:09
  • If you don't understand the API of wiredtiger, I recommend you have a look at how sophia database system does handle the same semantic. – amirouche Jun 25 '17 at 20:11