INF: Syntax for a Function Returning a Pointer to a Function

ID Number: Q83949

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

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.

The prototype for _dos_getvect in "The Microsoft C/C++ Run-Time

Library Reference" and in the DOS.H header file will provide another

example of this.

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

void afunc( char *cp, int ivar )

{

printf( "%s : int=%d\n", 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)( "Test1", 0 ); /* and calling the functions through them */

fp1 = retfunc2();

(*fp1)( "Test2", 1 );

fp2 = retfunc1();

(*fp2)( "Test3", 2 );

fp2 = retfunc2();

(*fp2)( "Test4", 3 );

}

Additional reference words: 2.00 2.01 2.50 2.51