Mail Archives: djgpp/1997/08/04/15:33:55
Burton Radons/Lothloriel, Hollow Dreams (lothloriel AT bc1 DOT com) writes:
> Base function:
>
> char *rsprintf(const char *format, ...)
> {
> va_list args;
> char *buffer;
> /* and on and on... */
> }
whatevertype myfunction (whatever, whatever, whatever, ...) {
// One of the whatevers should provide the function with enough info to
// know how many parameters to expect
va_list param_pt; // Declare the arg list variable
va_start(param_pt, number); // Call the setup macro
cout << "The parameters are "; // Example code that does something
for (int index = 0 ; index < number ; index++) // with params
{
cout << va_arg(param_pt, int) << " "; // Extract a parameter
}
cout << "\n";
va_end(param_pt); // Closing macro
}
There are five parts to varargs use. Part one: ... in the args of the
function, at the end. Part two: declaring a variable of va_list type.
Part three: determining the number of arguments (printf looks for the
number of format specifiers in the format string for instance) and calling:
va_start(the_va_list_variable, number_of_arguments_expected)
Part four: extracting a parameter. This involves determining what data
type to expect and using the value of:
va_arg(the_va_list_variable,the_expected_data_type)
You may be thinking "I never saw a function that took a type for an arg
before!" You still haven't; it's actually a macro in stdarg.h, which you
should #include.
Part five: after you are done with arguments, call:
va_end(the_va_list_variable)
and that's it.
Also, try looking at the source for such library functions as printf,
which use varargs.
--
.*. Where feelings are concerned, answers are rarely simple [GeneDeWeese]
-() < When I go to the theater, I always go straight to the "bag and mix"
`*' bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh
- Raw text -