INF: Declaring an Array of Pointers to Functions

ID Number: Q30580

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

The following program is an example of building an array containing

function addresses and calling those functions:

This information applies to Microsoft C versions 3.0, 4.0, 5.0, 5.1,

6.0, 6.0a, 6.0ax, and C/C++ 7.0 for MS-DOS systems, and C 5.1, 6.0,

and 6.0a for OS/2.

Sample Code

-----------

test1() ;

test2() ; /* Prototypes */

test3() ;

/* array with three functions */

int (*functptr[])() = { test1, test2, test3 } ;

main()

{

(*functptr[0])() ; /* Call first function */

(*functptr[1])() ; /* Call second function */

(*functptr[2])() ; /* Call third function */

}

test1()

{

printf ("hello 0\n") ;

}

test2()

{

printf ("hello 1\n") ;

}

test3()

{

printf ("hello 2\n") ;

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00