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

Last reviewed: September 4, 1997
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 "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 );
   }
Keywords          : CLngIss kbcode kbfasttip
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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 4, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.