我有这个功能:void func(int a, int b, char s, FILE *fp, int flag)
我想使用基于标志的函数参数。例如:func(num, NOTHING, NOTHING, file, 1)
func(num, othernum, NOTHING, NOTHING, 2)
并且在函数this_is_function
上,我希望具有:
void func(int a, int b, char s, FILE *fp, int flag){
if(flag == 1){
/* use a and file */
}
if(flag == 2){
/* use a,b */
}
/* etc etc */
}
我想知道是否有可能,如何做到这一点!提前谢谢:)
最佳答案
当您调用函数且不使用参数时,可以将指针的0
传递给int
和char
以及NULL
和。
func(num, 0, 0, file, 1)
func(num, othernum, 0, NULL, 2)
也可以使用变量函数。可变函数是具有可变参数数的函数。