PRODUCT : Borland C++ NUMBER : 1533 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/3 TITLE : Example of how to use variable function arguments /* Example of how to use functions which take a variable number of arguments. */ #include /* Refer to varargs.h for the macros and types used in this example. */ #include #define INTARG 1 #define DBLARG 2 #define STRARG 3 void printargs ( va_list va_alist, ... ) /* The first parameter must be declared exactly as seen here because the varargs.h macros used in this function depend on this hardcoded type and variable name. va_alist is a null-terminated array which indicates to this function what type of argument was pushed on the stack. In this example va_alist is a pointer to an array of int. */ { va_list ap; // ap is declared a 'void _FAR *'. This is // another hardcoded variable name which // must be used because macros in varargs.h // depend on 'ap'. int argtype, // argtype is an int, because that's what // the va_alist contains pointers to. *argtypep; // used to point to each int in va_alist. va_start (ap); // Points ap to the first element in the // va_alist. /* Point argtypep to the first element in va_alist. All of the elements of va_alist in this example are integers, but they don't have to be. */ argtypep = va_arg (ap, int *); PRODUCT : Borland C++ NUMBER : 1533 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/3 TITLE : Example of how to use variable function arguments /* Walk through the va_alist array until we hit the NULL. */ while ((argtype = *argtypep++) != 0) { /* Refer to the next argument in variable parameter list as the type indicated by va_alist. */ switch (argtype) { case INTARG: printf ("int : %d\n", va_arg (ap, int)); break; case DBLARG: printf ("double: %f\n", va_arg (ap, double)); break; case STRARG: printf ("string: %s\n", va_arg (ap, char *)); break; default: printf ("Can't handle argument\n"); break; } } /* Use va_end as a precaution against accidentally accessing va_alist without resetting with va_start. */ va_end (ap); } /* This null-terminated array holds the types of the arguments we're passing. This could also be built at run-time if you didn't know ahead of time what types the arguments would be. */ int at[] = {INTARG, DBLARG, STRARG, INTARG, DBLARG, STRARG, 0}; int main () { printargs (&at[0], 1, 2.0, "HELLO", 3, 4.0, "WORLD"); return 0; PRODUCT : Borland C++ NUMBER : 1533 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/3 TITLE : Example of how to use variable function arguments } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.