c - 如何用 C 语言编写带有可变参数的函数

标签 c user-defined-functions ellipsis

我想在 c 中编写一个参数数量可变的函数..有人可以指导我吗..

最佳答案

这样的函数调用省略号:http://www.learncpp.com/cpp-tutorial/714-ellipses-and-why-to-avoid-them/

Because ellipses are rarely used, dangerous, and we strongly recommend avoiding their use, this section can be considered optional reading.

如果您仍然需要这样的函数,请看一下这个示例(重点是 va_list):

double FindAverage(int nCount, ...)
{
    long lSum = 0;

    // We access the ellipses through a va_list, so let's declare one
    va_list list;

    // We initialize the va_list using va_start.  The first parameter is
    // the list to initialize.  The second parameter is the last non-ellipse
    // parameter.
    va_start(list, nCount);

    // Loop nCount times
    for (int nArg=0; nArg < nCount; nArg++)
         // We use va_arg to get parameters out of our ellipses
         // The first parameter is the va_list we're using
         // The second parameter is the type of the parameter
         lSum += va_arg(list, int);

    // Cleanup the va_list when we're done.
    va_end(list);

    return static_cast<double>(lSum) / nCount;
}

关于c - 如何用 C 语言编写带有可变参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15923834/

相关文章:

elasticsearch - 路线图中的UDF或概率数据结构

python - PySpark UDF 返回可变大小的元组

r - 如何让 R 识别省略号中的参数向量?

java - ScalaFX/JavaFX : How can I change the overrun style of a ComboBox?

c - GTK C "Could not find signal handler ''。你是用-rdynamic编译的吗? ”

c - 如何在编译时选择函数行为?

magento - 用户定义的支付 magento

jquery - chrome 中带省略号的多行文本问题

c - 实现公平硬币翻转

c - 我怎样才能将我的数据从我的缓冲区中获取到这个结构中?