Variable arguments to & from functions.
der Mouse
mouse at thunder.mcrcim.mcgill.edu
Tue Jun 18 16:18:08 AEST 1991
In article <1991Jun17.205314.25401 at cbnewsm.att.com>, akshay at cbnewsm.att.com (akshay.kumar.deshpande) writes:
> I am trying to write a variable argument function which in turn calls
> another variable argument function. Can anybody tell me how to pass
> the unknown arguments I receive to the next function.
(Questions generally end with `?' instead of `.'.)
> Here is a psuedo example:
[compressed -dM]
> A(int arg1, ...) {
> va_start(ap, fmt); /* from K&R 2nd edition - p 156 */
> B(arg1, arg2, arg3, <...>);
> }
> B(int arg1, int arg2, int arg3, ...); { ... }
> I would like to have the "..." of A match that for the function call
> of B.
Sorry, can't be done. You have to provide a v- form of B that takes a
va_list parameter and then call that from A. (You should probably do
this anyway; in fact, a normal thing to do would be to have B just call
its varargs version.)
Note also that va_start takes the name of the last fixed argument,
which is arg1, not fmt. (You can't just blindly copy code from K&R;
you have to understand it first, if only minimally.)
Something like (declarations omitted; also don't forget the va_end()
calls)
A(int arg1, ...) {
va_start(ap, arg1);
vB(arg1, arg2, arg3, ap);
}
B(int arg1, int arg2, int arg3, ...); {
va_start(ap,arg3);
vB(arg1,arg2,arg3,ap);
}
vB(int arg1, int arg2, int arg3, va_list ap) {
...do the real B here...
}
der Mouse
old: mcgill-vision!mouse
new: mouse at larry.mcrcim.mcgill.edu
More information about the Comp.lang.c
mailing list