Microsoft KB Archive/83949

{| = INFO: Syntax for a Function Returning a Pointer to a Function =
 * width="100%"|

Article ID: Q83949

The information in this article applies to:
 * Microsoft C compiler for MS-DOS, versions 5.1, 6.0, 6.0a, 6.0ax
 * Microsoft C/C++ compiler for MS-DOS, version 7.0
 * Microsoft Visual C++ for Windows, versions 1.0, 1.5
 * Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0

SUMMARY
The syntax for a function returning a pointer to another function is somewhat complicated due to the precedence rules involved. The syntax can be greatly simplified with the use of typedefs, but typedefs are not required. The following discussion applies to ANSI C and to C++.

MORE INFORMATION
The simplest way to define such a function is by using a typedef as an alias for a pointer to a function of the desired type. This is the recommended method. For example, consider the following statements:

typedef void (*FPN)( int ); FPN return_func_ptr( void ); The first statement defines FPN to be an alias for a pointer to a function taking an int and returning nothing. The second statement declares return_func_ptr to be a function taking nothing and returning a variable of type FPN; that is, returning a pointer to a function taking an int and returning nothing. To declare such a function without using a typedef, the syntax is rather different:

void (* (return_func_ptr(void)) )( int ) {     return a_func;  /* where a_func is a defined function */ } This defines return_func_ptr to be a function that takes nothing, and returns a pointer to a function taking an int and returning nothing. The syntax for calling a function through a pointer is shown in the following example:

void (*func_ptr)( int );    /* Define a pointer to a function taking an int and returning void */ func_ptr = return_func_ptr; (*func_ptr)( 3 );           /* Dereference it to call function  */ The following sample code further illustrates returning pointers to functions and using them to call the functions to which they point. For the 16-bit compilers listed above, the prototype for _dos_getvect in &quot;The Microsoft C/C++ Run-Time Library Reference&quot; and in the DOS.H header file will provide another example of this.

Sample Code
/* Compile options needed: none */

#include 

void afunc( char *cp, int ivar ) {     printf( &quot;%s : int=%d\n&quot;, cp, ivar ); }

/* Typedef FPN as a pointer to a function taking a char pointer and an int and returning nothing. */   typedef void (*FPN)( char *, int );

/* Define a function with a void parameter list returning a pointer to     a function taking a character pointer and an int and returning nothing. */

void (* (retfunc1(void)) )( char *, int ) {     return afunc; }

/* Define a function exactly the same as retfunc1, but using the FPN typedef previously defined to simplify the code. */

FPN retfunc2( void ) {     return afunc; }

void main( void ) {     void (*fp1)(char *, int);   /* fp1 and fp2 are both pointers to */ FPN fp2;                   /* functions taking a char pointer  */ /* and an int and returning nothing */

fp1 = retfunc1;     /* Examples of setting function pointers  */ (*fp1)( &quot;Test1&quot;, 0 ); /* and calling the functions through them */

fp1 = retfunc2; (*fp1)( &quot;Test2&quot;, 1 );

fp2 = retfunc1; (*fp2)( &quot;Test3&quot;, 2 );

fp2 = retfunc2; (*fp2)( &quot;Test4&quot;, 3 ); } Keywords          : kbcode kbLangC kbVC Version          : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS  NT:1.0,2.0,2.1,4.0,5.0 Platform         : MS-DOS NT WINDOWS Issue type       : kbinfo
 * }