I have a function foo(char *n, ...);
I need to get and use all of optional char
parameters.
I had an idea to use
while(va_arg(argP
The basic idea would work. But you've filled in the details in a way that almost certainly won't.
The usual implicit conversion rules don't apply when you're using variadic arguments. Instead, default argument promotions take place, which means that any integer type smaller than int
/unsigned int
gets converted to one of those -- that's not the only promotion, but the only one relevant here -- and which also means that there is no automatic conversion to whatever type you specify with va_arg
.
This means that:
va_arg(..., char)
, since it's impossible to pass a char
as a variadic function argument.NULL
as a variadic function argument, since its concrete type is heavily implementation-dependent. Realistic types are int
, long
, void *
, and there are loads of other less realistic but equally valid types.You could change
while(va_arg(argPtr, char) != NULL)
to
while(va_arg(argPtr, int) != 0)
and the call
foo(n, 't', 'm', '$', NULL);
to
foo(n, 't', 'm', '$', 0);