-2

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;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
jack_h
  • 1
  • 1
  • Welcome to SO. There is no standard mechanism for that. You have to implement your own. You can provide the number of arguments in the fixed part or some format string or you can define a specific value inditating the last argument – Gerhardh Sep 17 '21 at 14:17
  • 1
    Typically, the number of arguments will depend on `arg0` and `arg1`. – William Pursell Sep 17 '21 at 14:17
  • 5
    Does this answer your question? [How does Variable length argument works in C?](https://stackoverflow.com/questions/56952717/how-does-variable-length-argument-works-in-c) – Mark Benningfield Sep 17 '21 at 14:18

1 Answers1

1

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350