在C语言中,将一组变量从一个函数传递到一个宏
我看到了这个链接。将其传递给宏的语法是什么在C语言中,将一组变量从一个函数传递到一个宏,c,C,我看到了这个链接。将其传递给宏的语法是什么 #include <stdarg.h> #define exampleW(int b, args...) function2(b, va_args) static void exampleV(int b, va_list args); void exampleB(int b, ...) { va_list args; va_start(args, b); exampleV(b, args); //also
#include <stdarg.h>
#define exampleW(int b, args...) function2(b, va_args)
static void exampleV(int b, va_list args);
void exampleB(int b, ...)
{
va_list args;
va_start(args, b);
exampleV(b, args);
//also pass args to a macro which takes multiple args after this step
??? [Is it exampleW(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...
}
这是不可能的。宏是在编译时展开的,因此在使用宏的编译时需要知道它们的参数。函数exampleB的参数通常直到运行时才知道。即使在许多情况下,编译函数调用时可能知道参数,但这些参数可能位于不同的源文件中,这对您处理此源文件中的宏没有帮助 您需要: 让exampleB调用vfunction2这样的函数,该函数被function2重写以获取va_列表参数 作为宏重新实现示例B 如果exampleB有有限个可能的参数组合,则编写代码分别处理所有情况: 执行一些不可移植的操作,使用与传递给exampleB相同的参数调用函数function2,例如使用汇编语言技巧或gcc。
宏获取源代码片段,并在编译时对其进行操作。va_列表表示数字和指针等值,并在程序运行时帮助访问它们。你似乎想要一些东西来混合这些类别,并按照它们发生的唯一顺序使用它们。你是说这个定义函数。。。function2\uuuu VA\u ARGS\uuuuuu?为了澄清我的问题,我需要将一组变量参数从一个函数传递给一个名为inside的宏,该函数本身已将变量ARGS作为输入。
if (b == TWO_INTS) {
int i1 = va_arg(args, int);
int i2 = va_arg(args, int);
exampleW(i1, i2);
} else if (b == STRING_AND_DOUBLE) {
char *s = va_arg(args, char *);
double d = va_arg(args, double);
exampleW(s,d);
} else // ...