I had read the following about passing arguments to variadic functions,
/a/3530807/22299201
#include<stdio.h>
#include<string.h>
#include<stdarg.h>
#define BUF_SIZE 1024
void foo(char*, ...);
void wrapper(char*, va_list);
void bar(char*, ...);
int main() {
char *format = "Unit testing this function %s";
foo(format, "args1");
return 0;
}
void foo(char *format, ...) {
// code to capture output
va_list args;
va_start(args, format);
char *temp = va_arg(args, char*);
printf("args foo %sn", temp);
wrapper(format, args);
va_end(args);
// code to capture output
}
void bar(char *format, ...) {
char buffer[BUF_SIZE];
va_list args;
va_start(args, format);
char *temp = va_arg(args, char*);
printf("args bar %sn", temp);
vsprintf(buffer, format, args);
printf("%sn", buffer);
va_end(args);
}
void wrapper(char* format, va_list args) {
bar(format, args);
char *temp = va_arg(args, char*);
printf("args wrapper %sn", temp);
}
Output:
args foo args1
args bar
Unit testing this function
args wrapper args1
Let me explain the scenario:-
- I have to unit test a production function with a similar signature and logic as
bar
. - I am using acutest framework to test my code,
main
here can resemble that test function. - I am using a helper function,
foo
that has some logic to capture output ofbar
. - Based on the above link shared, it is impossible to pass variable arguments from
foo
tobar
, hence passing to another wrapper func.
Why are args
not being propogated to bar
?
What am I doing wrong?