这里列出的两个东西不经常使用,很容易忘记。其中含有可变参数的函数的部分来自CSDN论坛。
#include
<
iostream
>
#include < string >
#include < stdarg.h >
using namespace std;
int foo( const int )
... {
printf("int n ");
return 1;
}
void foo( int ( * p)( int ))
... {
p(1);
printf("foo ");
}
void f( const char * str, ...)
... {
va_list ap;
char* s = 0;
int d = 0;
double f = 0.0;
va_start(ap, str);
s = va_arg(ap, char*);
d = va_arg(ap, int);
f = va_arg(ap, double);
va_end(ap);
printf("%s, %s, %d, %f", str, s, d, f);
}
void main()
... {
int(*p)(const int) = foo;
foo(p);
foo(p(1)); //注意这个调用和上面那个的区别
f("function", "varargs", 1, 1.0);
getchar();
}
#include < string >
#include < stdarg.h >
using namespace std;
int foo( const int )
... {
printf("int n ");
return 1;
}
void foo( int ( * p)( int ))
... {
p(1);
printf("foo ");
}
void f( const char * str, ...)
... {
va_list ap;
char* s = 0;
int d = 0;
double f = 0.0;
va_start(ap, str);
s = va_arg(ap, char*);
d = va_arg(ap, int);
f = va_arg(ap, double);
va_end(ap);
printf("%s, %s, %d, %f", str, s, d, f);
}
void main()
... {
int(*p)(const int) = foo;
foo(p);
foo(p(1)); //注意这个调用和上面那个的区别
f("function", "varargs", 1, 1.0);
getchar();
}
执行结果为:
int n
foo
int n
int n
function, varargs, 1, 1.000000
在无法列举出传递给函数的所有实参的类型和数目时,可以使用省略符形参。省略符暂停了类型检查机制。它们的出现告知编译器,当调用函数时,可以有0个或多个实参,而实参的类型未知。两种形式:
void foo(parm_list, ...); //逗号可选,这个规定很麻烦
void foo(...); //现在还不知道这种形式如何使用,待查。。。