元の製品バージョン: Visual C++
元の KB 番号: 30580
この記事では、Visual C++ で関数へのポインターの配列を宣言する方法について説明します。 この記事の情報は、アンマネージ Visual C++ コードにのみ適用されます。
次のサンプル コードは、関数アドレスを含む配列を構築し、それらの関数を呼び出す方法を示しています。
/*
* 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");
}