Reputation: 29407
In this example this code although compiles it doesn't produce correct result:
void encapsulatePrintf(const char *str, ...) {
va_list argptr;
va_start(argptr, str);
printf(str, argptr);
va_end(argptr);
}
and then in main: encapsulatePrintf("test str: %i - %s", 22, "test2");
test str: 2293428 - á "
but when I change the function from printf
to vfprintf(stdout, str, argptr);
What's going on here ?
Upvotes: 3
Views: 175
Reputation: 154025
printf()
doesn't take va_list
as argument but rather a variable list of arguments while vprintf()
takes a va_list
as argument and not a variable list of arguments. Basically, when you called printf()
using
printf(str, argptr);
you invoked undefined behavior: the first argument promised that you would pass an int
and a char*
but you passed, instead, a va_list
. The types of the arguments passed to printf()
have to match the specification in the format string.
Upvotes: 8