va_list 中传递的参数数量 [英] number of passed arguments in a va_list
本文介绍了va_list 中传递的参数数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部,
我想控制 va_list 中传递参数的数量
.
I want to control the number of passed parameters
in a va_list.
va_list args;
va_start(args, fmts);
vfprintf(stdout, fmts, args);
va_end(args);
是否有可能在 va_start
之后立即获得参数的数量?
Is there any possibility to get the number of parameters just after a va_start
?
推荐答案
不完全是你想要的,但是你可以使用这个宏来计算参数
Not exactly what you want, but you can use this macro to count params
#include <stdio.h>
#include <stdarg.h>
#define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
#define NARGS(...) NARGS_SEQ(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define fn(...) fn(NARGS(__VA_ARGS__) - 1, __VA_ARGS__)
static void (fn)(int n, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("%d params received\n", n);
vprintf(fmt, args);
va_end(args);
}
int main(void)
{
fn("%s %d %f\n", "Hello", 7, 5.1);
return 0;
}
这篇关于va_list 中传递的参数数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文