If I have a C function where one argument is "...", how do I determine if more than two arguments are being passed to that function? If the third argument being sent to my_func()
would be int arg2
, how do I access it?
#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;
}
In C, you can't deduce how many arguments are sent to the function from the arguments alone. You need some supporting information, usually passed in one of the arguments themselves. E.g., for the well-known printf
, the first argument is a format string, and parsing it will tell the function how many other arguments it should take and their types.