共用方式為


在 Visual C++ 中宣告函式的指標陣列

原始產品版本: Visual C++
原始 KB 編號: 30580

本文介紹如何在 Visual C++ 中宣告函式的指標陣列。 本文中的資訊僅適用於 Unmanaged 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");
}