1

Is there any way to call a variadic function from another one?

I know that if I want to forward a variadic call, I can use a function that accepts va_list parameters.

For example, If I declare my_printf I can call vfprintf inside.

Another way would be to use macros:

#define my_printf(a, ...) fprintf(a, __VA_ARGS__)

but is there a way to "create" a variadic call without being forced to use va_list parameters?

In other words: is there any way (standard or not, using gcc extensions, etc.) that my_printf can call fprintf directly and not vfprintf?

mastupristi
  • 1,240
  • 1
  • 13
  • 29
  • The C standard does not provide any facility for this. – Eric Postpischil Feb 18 '21 at 13:32
  • 1
    The best solution is to avoid anything variadic in the first place. It's a horrible and dangerous API. – Lundin Feb 18 '21 at 14:15
  • Can you describe what real world problem you are attempting to solve using this method? Also, have you seen [this question/many answers]? I will vote to close this question, not because it isn't a good question, but because it has been asked and answered [here](https://stackoverflow.com/questions/150543/forward-an-invocation-of-a-variadic-function-in-c). If you can explain how your request distinguishes itself from the linked post, please do, and If indeed you can do that, I will remove the close vote. – ryyker Feb 18 '21 at 14:23

1 Answers1

2

Is there any way to call a variadic function from another one?

Yes, use va_list. It's the way to call another variadic function, standardized and supported everywhere. When providing a variadic function from your library, make sure you provide va_list version as well. Do not use other options, unless you definitely have to.

is there any way (standard

No.

Well, there is super old POSIX varargs.h.

using gcc extensions

There are some builtin functions: __builtin_va_arg_pack() stuff and __builtin_apply() stuff.

etc.)

Write some assembly to forward the arguments.

There are FFI libraries available.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111