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

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
*/

#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");
}
Proprietà

ID articolo: 30580 - Ultima revisione: 09 gen 2006 - Revisione: 1

Feedback