Microsoft KB Archive/30580

= How to declare an array of pointers to functions in Visual C++ =

Article ID: 30580

Article Last Modified on 1/9/2006

-

APPLIES TO


 * Microsoft C Professional Development System 6.0a
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q30580





Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SUMMARY
The sample below demonstrates building an array that contains function addresses and calling those functions.

Sample Code
/* * Compile options needed: none */


 * 1) include 

void test1; void test2;           /*  Prototypes */ void test3;

/* array with three functions */ void (*functptr[]) = { test1, test2, test3 } ;

void main {  (*functptr[0]);    /*  Call first function  */ (*functptr[1]);   /*  Call second function */ (*functptr[2]);   /*  Call third function  */ }

void test1 {  printf(&quot;hello 0\n&quot;); }

void test2 {  printf(&quot;hello 1\n&quot;); }

void test3 {  printf(&quot;hello 2\n&quot;); }

Keywords: kbhowto kbinfo kblangc KB30580

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.