Article ID: 30580 - Last Review: January 9, 2006 - Revision: 4.0

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

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.

On This Page

Expand all | Collapse all

SUMMARY

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

Sample Code

/*
 * Compile options needed: none
 */ 

#include <stdio.h>

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("hello 0\n");
}

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

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

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++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowto kbinfo kblangc KB30580