如何在 C 中定义函数指针数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5488608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How define an array of function pointers in C
提问by strstr
I've a little question.
I'm trying to define an array of function pointers dynamically with calloc
.
But I don't know how to write the syntax.
Thanks a lot.
我有一个小问题。我正在尝试使用calloc
. 但我不知道如何编写语法。非常感谢。
回答by Andrew Eidsness
The type of a function pointer is just like the function declaration, but with "(*)" in place of the function name. So a pointer to:
函数指针的类型就像函数声明一样,只是用“(*)”代替了函数名。所以指向:
int foo( int )
would be:
将是:
int (*)( int )
In order to name an instance of this type, put the name inside (*), after the star, so:
为了命名这种类型的实例,将名称放在 (*) 内,在星号之后,因此:
int (*foo_ptr)( int )
declares a variable called foo_ptr that points to a function of this type.
声明一个名为 foo_ptr 的变量,该变量指向此类型的函数。
Arrays follow the normal C syntax of putting the brackets near the variable's identifier, so:
数组遵循将括号放在变量标识符附近的正常 C 语法,因此:
int (*foo_ptr_array[2])( int )
declares a variable called foo_ptr_array which is an array of 2 function pointers.
声明了一个名为 foo_ptr_array 的变量,它是一个包含 2 个函数指针的数组。
The syntax can get pretty messy, so it's often easier to make a typedef to the function pointer and then declare an array of those instead:
语法可能会变得非常混乱,因此通常更容易为函数指针创建 typedef,然后声明一个数组:
typedef int (*foo_ptr_t)( int );
foo_ptr_t foo_ptr_array[2];
In either sample you can do things like:
在任一示例中,您都可以执行以下操作:
int f1( int );
int f2( int );
foo_ptr_array[0] = f1;
foo_ptr_array[1] = f2;
foo_ptr_array[0]( 1 );
Finally, you can dynamically allocate an array with either of:
最后,您可以使用以下任一方式动态分配数组:
int (**a1)( int ) = calloc( 2, sizeof( int (*)( int ) ) );
foo_ptr_t * a2 = calloc( 2, sizeof( foo_ptr_t ) );
Notice the extra * in the first line to declare a1 as a pointer to the function pointer.
注意第一行中额外的 * 将 a1 声明为指向函数指针的指针。
回答by Andres
I put a small example here that may help you
我在这里放了一个小例子,可能对你有帮助
typedef void (*fp)(int); //Declares a type of a void function that accepts an int
void test(int i)
{
printf("%d", i);
}
int _tmain(int argc, _TCHAR* argv[])
{
fp function_array[10]; //declares the array
function_array[0] = test; //assings a function that implements that signature in the first position
function_array[0](10); //call the cuntion passing 10
}
回答by John Bode
You'd declare an array of function pointers as
您可以将函数指针数组声明为
T (*afp[N])();
for some type T
. Since you're dynamically allocating the array, you'd do something like
对于某些类型T
。由于您正在动态分配数组,因此您可以执行以下操作
T (**pfp)() = calloc(num_elements, sizeof *pfp);
or
或者
T (**pfp)() = malloc(num_elements * sizeof *pfp);
You'd then call each function as
然后你将每个函数称为
T x = (*pfp[i])();
or
或者
T x = pfp[i](); // pfp[i] is implicitly dereferenced
If you want to be unorthodox, you can declare a pointer to an array of pointers to functions, and then allocate that as follows:
如果你想变得非正统,你可以声明一个指向函数指针数组的指针,然后按如下方式分配它:
T (*(*pafp)[N])() = malloc(sizeof *pafp);
although you would have to deference the array pointer when making the call:
尽管您在调用时必须遵循数组指针:
x = (*(*pafp)[i])();
回答by pmg
Assuming all your functions are of type void ()(void)
, something like this
假设你所有的函数都是 type void ()(void)
,像这样
typedef void (*fxptr)(void);
fxptr *ptr; // pointer to function pointer
ptr = malloc(100 * sizeof *ptr);
if (ptr) {
ptr[0] = fx0;
ptr[1] = fx1;
/* ... */
ptr[99] = fx100;
/* use "dynamic array" of function pointers */
free(ptr);
}
回答by Armen Tsirunyan
typedef R (*fptr)(A1, A2... An);
where R is the return type, A1, A2... An are the argument types.
其中 R 是返回类型,A1、A2... An 是参数类型。
fptr* arr = calloc(num_of_elements,sizeof(fptr));