【发布时间】:2021-11-12 10:05:15
【问题描述】:
如果我有一个 C 函数,其中一个参数是“...”,我如何确定是否将两个以上的参数传递给该函数?如果发送到my_func() 的第三个参数是int arg2,我该如何访问它?
#include <stdio.h>
int my_func(int arg0, int arg1, ...)
{
/* How can I determine if 2 or 3 arguemnts are being passed to
* this function? How do I access the third argument so I could
* print it out? */
return 0;
}
int main() {
my_func(1,2);
my_func(1,2,3);
return 0;
}
【问题讨论】:
-
欢迎来到 SO。没有标准的机制。你必须实现你自己的。您可以在固定部分或某些格式字符串中提供参数的数量,或者您可以定义一个特定的值来指示最后一个参数
-
通常,参数的数量取决于
arg0和arg1。
标签: c arguments variadic-functions c99