6

Recently I found function prototype with three dots argument. I wrote my own function and it compiled well:

void func(int a, ...){}

What does that mean?

update

Thank you guys! I figured it out. Here's my example:

void func(unsigned int n_args, int arg, ...)
{
    for(unsigned int i = 0; i < n_args; ++i)
        cout << *((int*)&arg + i) << ' ';
}

This function prints out arguments separated by space character.

Ivars
  • 2,375
  • 7
  • 22
  • 31
  • [variadic functions](https://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=138) – Suvarna Pattayil Jan 03 '14 at 10:49
  • Its already discussed here [Variable number of arguments in C++?][1] [1]: http://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c – Boris Ivanov Jan 03 '14 at 10:49
  • 1
    I think that your way to iterate through `...` is UB (undefined behaviour). – Jarod42 Jan 03 '14 at 11:12
  • @Jarod42 you mean if we pass a different types of arument? i agree, but this was only an exaple which expects that all of passed arguments would be type of int. – Ivars Jan 03 '14 at 11:14
  • Prefer anyway to use `va_start`, `va_arg` and `va_end`. – Jarod42 Jan 03 '14 at 11:21
  • @Jarod42 ok. it's a little bit off-topic, but are there moments when we have to use variadic functions or at least when variadic functions makes our life easier? – Ivars Jan 03 '14 at 11:47
  • @user2543574: variadic functions are unsafe (no type check on `...`) by nature, the new ways in C++11 is variadic template (which is typed). So, variadic functions are(were) mainly useful for logging as `printf` (family). If you want only int in `...`, you may pass a `const std::vector&` (or similar). – Jarod42 Jan 03 '14 at 11:55

1 Answers1

6

A function with three dots means, that you can pass a variable number of arguments. Since the called function doesn't really know how many arguments were passed, you usually need some way to tell this. So some extra parameter would be needed which you can use to determine the arguments.

A good example would be printf. You can pass any number of arguments, and the first argument is a string, which describes the extra parameters being passed in.

void func(int count, ...)
{
    va_list args;
    int i;
    int sum = 0;

    va_start(args, count);
    for(i = 0; i < count; i++)
        sum += va_arg(args, int);
    va_end(ap);

    printf("%d\n", sum);
}

update

To address your comment, you don't need the names of the arguments. That is the whole point of it, because you don't know at compile time which and how many arguments you will pass. That depends on the function of course. In my above example, I was assuming that only ints are passed though. As you know from printf, you pass any type, and you have to interpret them. that is the reason why you need a format specifier that tells the function what kind of parameter is passed. Or as shown in my example you can of course assume a specific type and use that.

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • thanks. but how can i use that, since i don't even know a name of argumants? – Ivars Jan 03 '14 at 10:48
  • 1
    @user2543574, I suggest not using it, but preferring variadic templates instead. They're actually type safe, but be warned that they can be a little confusing at first (and well beyond). Variadic functions are also not beginner material, though. If you want an example of something that uses this, look at `printf`. To use it (or variadic templates) yourself, there are lots of resources out there. – chris Jan 03 '14 at 10:50
  • @user2543574, I added an example. – Devolus Jan 03 '14 at 10:53
  • @Devolus so it's like a dynamic array which is passed like an argument? – Ivars Jan 03 '14 at 10:59
  • Yes, its a bit similar. Like an array of unknown types, but of course you can not really access it like an array, so this woul dbe only an analogy. Theorethically you can achieve a similar effect by constructing a dynamic array and then cast all the parameters depending on your need. Using a variadic function is easier to read though. – Devolus Jan 03 '14 at 11:01