Passing Variable Arguments Between Functions in C
This question pertains to passing variable arguments to another function that also accepts a variable argument list. The task involves invoking exampleB from example, while preserving the variable argument list in exampleB.
Directly passing the arguments is not feasible. Instead, an intermediary function that accepts a variable argument list is required. Here's how it can be accomplished:
#include <stdarg.h> static void exampleV(int b, va_list args); // Intermediary function void example(int a, int b, ...) // Renamed for consistency { va_list args; do_something(a); // Use argument a 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... // Excluding va_start and va_end }
In this setup, exampleV acts as a bridge, passing the variable arguments from example to exampleB without making modifications to exampleB.
The above is the detailed content of How Can I Pass Variable Arguments from One Function to Another in C?. For more information, please follow other related articles on the PHP Chinese website!