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

后端 未结 6 624
借酒劲吻你
借酒劲吻你 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: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 
    #include 
    
    int sum(int count, ...)
    {
        int total, i, temp;
        total = 0;
        va_list args;
        va_start(args, count);
        for(i=0; i

    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

提交回复
热议问题