When working with functions that employ variable argument lists, the challenge of transmitting arguments from one function to another that accepts a similar list can arise.
Consider the following example:
void example(int a, int b, ...); void exampleB(int b, ...);
Here, example calls exampleB. A key question is how to pass the variables from the variable argument list in example to exampleB without modifying exampleB (which may be used in other contexts).
Unfortunately, direct passing of variable arguments between functions is not possible. A custom solution is required to achieve this.
To bridge this gap, we can introduce a helper function that accepts a va_list as an argument:
#include <stdarg.h> static void exampleV(int b, va_list args);
exampleA (renamed for consistency) is modified to utilize this helper function:
void exampleA(int a, int b, ...) { va_list args; do_something(a); // Use argument a somehow va_start(args, b); exampleV(b, args); va_end(args); }
exampleB can also use the same helper function without any modifications:
void exampleB(int b, ...) { va_list args; va_start(args, b); exampleV(b, args); va_end(args); }
Within exampleV, the actual operations that were intended to be performed in exampleB can be executed, without the need for va_start or va_end.
static void exampleV(int b, va_list args) { ...whatever you planned to have exampleB do... ...except it calls neither va_start nor va_end... }
By employing this approach, we can effectively pass variable arguments from one function to another, without altering the second function's original implementation.
The above is the detailed content of How Can I Pass Variable Arguments from One Function to Another with Variable Argument Lists?. For more information, please follow other related articles on the PHP Chinese website!