在C函数声明中,“...”作为最后一个参数做什么?

时间:2022-08-27 00:06:13

Often I see a function declared like this:

我常常看到一个声明如下的函数:

void Feeder(char *buff, ...)

what does "..." mean?

这是什么意思?

5 个解决方案

#1


24  

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

它允许可变数量的未指定类型的参数(如printf一样)。

you have to access them with va_start, va_arg and va_end

你必须使用va_start,va_arg和va_end访问它们

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

有关详细信息,请参阅http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

#2


16  

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.

变量函数是可以采用可变数量的参数的函数,并使用省略号代替最后一个参数进行声明。这种功能的一个例子是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.

C中不允许

#3


6  

It means that a variadic function is being declared.

这意味着声明了一个可变函数。

#4


6  

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


3  

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

#1


24  

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

它允许可变数量的未指定类型的参数(如printf一样)。

you have to access them with va_start, va_arg and va_end

你必须使用va_start,va_arg和va_end访问它们

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

有关详细信息,请参阅http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

#2


16  

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.

变量函数是可以采用可变数量的参数的函数,并使用省略号代替最后一个参数进行声明。这种功能的一个例子是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.

C中不允许

#3


6  

It means that a variadic function is being declared.

这意味着声明了一个可变函数。

#4


6  

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


3  

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