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

一曲冷凌霜 提交于 2019-12-17 08:48:05

问题


Often I see a function declared like this:

void Feeder(char *buff, ...)

what does "..." mean?


回答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




回答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.




回答3:


It means that a variadic function is being declared.




回答4:


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();



回答5:


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;
}



回答6:


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


来源:https://stackoverflow.com/questions/2735587/in-a-c-function-declaration-what-does-as-the-last-parameter-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!