[lug] variable length parameters
Kelly Brock
krbrock at pacbell.net
Thu May 3 14:59:46 MDT 2001
Howdy,
> This is a pure c programming question, if there is a better
> place to ask it,
> I don't know about it.
>
> I'm trying to build a few functions for error and status logging.
> What I want to do is take a variable length parameter list,
> act according to
> which function was called, and then pass the stuff on to
> printf or maybe my
> own bastardised printf.
>
> Question:
> can I have a function that takes variable parameters and pass those
> parameters on [with/out modification] to another function that takes
> variable parameters.
There is no direct method but the issue is resolved through use of va_list
arguments. Just put in the following change:
> my simple test:
> =========================================================
> #include<stdio.h>
> #include<stdarg.h>
>
> void test(char *fmt, ...)
> {
> va_list ap;
> va_start(ap, fmt);
> printf(fmt, ap);
change to:
vprintf(fmt, ap);
> va_end(ap);
> }
>
> int main (void)
> {
> int i=1;
>
> test("foo%dbar\n", i);
> }
> =========================================================
>
> my simple test bombs with:
> foo37813904bar
> as output.
>
> Any ideas on how to accomplish this?
Hope that helped.
Kelly Brock
Maxis - Electronic Arts
More information about the LUG
mailing list