C语言 在 C 函数声明中,“...”作为最后一个参数有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2735587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In a C function declaration, what does "..." as the last parameter do?
提问by alaamh
Often I see a function declared like this:
我经常看到这样声明的函数:
void Feeder(char *buff, ...)
what does "..." mean?
这是什么意思?
采纳答案by knittl
it allows a variable number of arguments of unspecified type (like printfdoes).
它允许可变数量的未指定类型的参数(就像printf那样)。
you have to access them with va_start, va_argand va_end
您必须使用va_start,va_arg和va_end
see http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.htmlfor more information
有关更多信息,请参阅http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html
回答by N 1.1
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.
可变参数函数是可以接受可变数量参数并用省略号代替最后一个参数声明的函数。这种函数的一个例子是
printf。一个典型的声明是
int check(int a, double b, ...);可变参数函数必须至少有一个命名参数,例如,
char *wrong(...);在 C 中是不允许的。
回答by dafmetal
The three dots '...' are called an ellipsis. Using them in a function makes that function a variadicfunction. 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();
回答by Ignacio Vazquez-Abrams
It means that a variadic functionis being declared.
这意味着正在声明一个可变参数函数。
回答by RvdK
variadic function (multiple parameters)
可变参数函数(多参数)
#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;
}
回答by arsho
Functions with ...as last parameter are called Variadic functions (Cppreference. 2016). This ...is used to allow variable length parameters with unspecified types.
以功能...作为最后一个参数被称为可变参数函数(Cppreference。2016)。这...用于允许具有未指定类型的可变长度参数。
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.
可变参数函数示例:让我们假设我们需要一个 sum 函数,它将返回可变数量参数的总和。我们可以在这里使用可变参数函数。
#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
练习题:一个简单的练习可变参数函数的问题可以在这里的hackerrank练习题中找到
Reference:
参考:
- Cppreference. (2016, February 13). Variadic functions. Retrieved July 25, 2018, from https://en.cppreference.com/w/c/variadic
- 优先级。(2016 年,2 月 13 日)。可变函数。2018 年 7 月 25 日检索自https://en.cppreference.com/w/c/variadic


