【发布时间】:2012-12-19 00:58:50
【问题描述】:
可能的重复:
C++ preprocessor __VA_ARGS__ number of arguments
How to count the number of arguments passed to a function that accepts a variable number of arguments?
为了学习变参函数,我写了一个demo:
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
int foo(int n, int m, ...);
int
main (int argc, char *argv[])
{
int n = 10;
int m = 15;
int p = 20;
//foo (n, m);
foo(n, m, 20);
return EXIT_SUCCESS;
}
int foo (int n, int m, ...)
{
va_list ap;
int p;
char *str;
va_start (ap, m);
p = va_arg (ap, int);
n = m + p;
printf ("%d.\n", n);
va_end (ap);
return 0;
}
如果函数只有两个参数,我想知道如何处理它。 (对于这个演示,如果只有n和m,在运行foo之后,我想得到结果:n = n+m。)
【问题讨论】:
-
@RedX:那个骗子是针对可变参数宏,而不是可变参数函数。
标签: c parameters