In a C function declaration, what does "..." as the last parameter do?

30,905

Solution 1

it allows a variable number of arguments of unspecified type (like printf does).

you have to access them with va_start, va_arg and va_end

see http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html for more information

Solution 2

Variadic functions

Variadic functions are functions which may take a variable number of arguments and are declared with an ellipsis in place of the last parameter. An example of such a function is printf.

A typical declaration is

    int check(int a, double b, ...);

Variadic functions must have at least one named parameter, so, for instance,

    char *wrong(...);  

is not allowed in C.

Solution 3

The three dots '...' are called an ellipsis. Using them in a function makes that function a variadic function. To use them in a function declaration means that the function will accept an arbitrary number of parameters after the ones already defined.

For example:

Feeder("abc");
Feeder("abc", "def");

are all valid function calls, however the following wouldn't be:

Feeder();

Solution 4

variadic function (multiple parameters)

wiki

#include <stdarg.h>

double average(int count, ...)
{
    va_list ap;
    int j;
    double tot = 0;
    va_start(ap, count); //Requires the last fixed parameter (to get the address)
    for(j=0; j<count; j++)
        tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
    va_end(ap);
    return tot/count;
}

Solution 5

It means that a variadic function is being declared.

Share:
30,905
alaamh
Author by

alaamh

Updated on July 09, 2022

Comments

  • alaamh
    alaamh almost 2 years

    Often I see a function declared like this:

    void Feeder(char *buff, ...)
    

    what does "..." mean?