In a C function declaration, what does “…” as the last parameter do?

后端 未结 6 607
借酒劲吻你
借酒劲吻你 2020-11-28 06:56

Often I see a function declared like this:

void Feeder(char *buff, ...)

what does \"...\" mean?

相关标签:
6条回答
  • 2020-11-28 07:11

    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

    0 讨论(0)
  • 2020-11-28 07:12

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 07:16

    Functions with ... as last parameter are called Variadic functions (Cppreference. 2016). This ... is used to allow variable length parameters with unspecified types.

    We can use variadic functions when we are not sure about the number of parameters or their types.

    Example of variadic function: Let us assume we need a sum function that will return the summation of variable number of arguments. We can use a variadic function here.

    #include <stdio.h>
    #include <stdarg.h>
    
    int sum(int count, ...)
    {
        int total, i, temp;
        total = 0;
        va_list args;
        va_start(args, count);
        for(i=0; i<count; i++)
        {
            temp = va_arg(args, int);
            total += temp;
        }
        va_end(args);
        return total;
    }
    
    int main()
    {
        int numbers[3] = {5, 10, 15};
        // Get summation of all variables of the array
        int sum_of_numbers = sum(3, numbers[0], numbers[1], numbers[2]);
        printf("Sum of the array %d\n", sum_of_numbers);
        // Get summation of last two numbers of the array
        int partial_sum_of_numbers = sum(2, numbers[1], numbers[2]);
        printf("Sum of the last two numbers of the array %d\n", partial_sum_of_numbers);
        return 0;
    }
    

    Output:

    Practice problem: A simple problem to practice variadic function can be found in hackerrank practice problem here

    Reference:

    • Cppreference. (2016, February 13). Variadic functions. Retrieved July 25, 2018, from https://en.cppreference.com/w/c/variadic
    0 讨论(0)
  • 2020-11-28 07:17

    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.

    0 讨论(0)
  • 2020-11-28 07:17

    It means that a variadic function is being declared.

    0 讨论(0)
  • 2020-11-28 07:21

    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();
    
    0 讨论(0)
提交回复
热议问题