本文介绍了将可变参数传递给接受可变参数列表的另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个函数都有类似的参数

So I have 2 functions that both have similar arguments

void example(int a, int b, ...);
void exampleB(int b, ...);

现在示例调用 exampleB ,但是如何传递变量参数列表中的变量而不修改 exampleB (因为这已经在其他地方使用过了)。

Now example calls exampleB, but how can I pass along the variables in the variable argument list without modifying exampleB (as this is already used elsewhere too).

推荐答案

你不能直接做;您必须创建一个包含 va_list 的函数:

You can't do it directly; you have to create a function that takes a va_list:

#include <stdarg.h>

static void exampleV(int b, va_list args);

void example(int a, int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

void exampleB(int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

static void exampleV(int b, va_list args)
{
    ...whatever you planned to have exampleB do...
    ...except it calls neither va_start nor va_end...
}

这篇关于将可变参数传递给接受可变参数列表的另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:51
查看更多