Home >Backend Development >C++ >How to Pass Variable Arguments Between C Functions with Variable Argument Lists?
Problem:
You have two functions, example() and exampleB(), with similar variable argument lists:
void example(int a, int b, ...); void exampleB(int b, ...);
You need to call example() from within exampleB() without modifying the latter's variable argument list, as it is used elsewhere.
Solution:
Unfortunately, there is no direct method to pass variable arguments in C. To achieve this, you must define a helper function that takes a va_list parameter. Here's an example:
#include <stdarg.h> static void exampleV(int b, va_list args); void exampleA(int a, int b, ...) // Renamed for consistency { va_list args; do_something(a); // Use argument a somehow va_start(args, b); exampleV(b, args); va_end(args); } void exampleB(int b, ...) { va_list args; va_start(args, b); exampleV(b, args); va_end(args); } static void exampleV(int b, va_list args) { ...whatever you planned to have exampleB do... ...except it calls neither va_start nor va_end... }
In this setup:
By adding exampleV() as an intermediary, you can pass the variable arguments to exampleB() without modifying its original implementation.
The above is the detailed content of How to Pass Variable Arguments Between C Functions with Variable Argument Lists?. For more information, please follow other related articles on the PHP Chinese website!