PRODUCT : Borland C++ NUMBER : 1548 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/2 TITLE : Function pointers and variable length argument lists /* * This example shows how to use pointers to functions within a * variable parameter list. A type for the function pointer must * be defined using 'typedef' in order to use function pointers * with a variable parameter list. The syntax for this is as * follows: * * typedef ( *) ( ); * * You would use the variable specified by to declare * instances of the function pointer in the same way that * 'int' is used to create instances of integers. Failing to use * the typedef will cause an Expression Syntax Error. * * See the online documentation on va_arg for more information on * using variable parameter list. */ #include #include /* Declare a new type: a function taking no parameters, * returning an integer. */ typedef int ( *type ) ( void ); /* A function which returns the value three. */ int three( void ) { printf( "In function three\n" ) ; return 3 ; } /* A function which returns the value ten. */ int ten( void ) { printf( "In function ten\n" ) ; return 10 ; } /* Calculate the sum of a 0 terminated list of functions PRODUCT : Borland C++ NUMBER : 1548 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/2 TITLE : Function pointers and variable length argument lists * which return integers. */ void sum(char *msg, ...) { int total = 0; va_list ap; type func ; // Pointer to the function va_start( ap, msg ); while (( func = va_arg( ap,type )) != 0) { total = total + func() ; // Compute total } printf( msg, total ) ; va_end(ap); } int main(void) { sum( "The total of the functions is %d\n", three, ten, 0); return 0; } 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.