简体   繁体   English

指向引用函数的指针数组的指针,如何更改指针值和调用函数?

[英]Pointer to an array of pointers referencing functions, how do I change pointer values and call functions?

int *(*(*functions_array)[4](); 
int *function() { /*code */ };

I know that with a Pointer referencing a function int *(*func)(); 我知道使用指针引用函数int *(*func)(); I can just assign pointer value of a function to func func = &function; 我可以将函数的指针值分配给func func = &function; and then call with func(); 然后调用func(); .

What's the closest I could get to this behavior but with addition of arrays ? 除了添加数组,我最接近这种行为的是什么?

You probably want this: 您可能想要这样:

#include <stdio.h>

int function1(void)
{
  printf("function 1\n");
  return 1;
};

int function2(void)
{
  printf("function 2\n");
  return 2;
};

int(*FunctionPointer)(void);             // a function pointer
int(*ArrayOfFunctionPointers[4])(void);  // an array of 4 function pointers


int main()
{
  FunctionPointer = function1;
  FunctionPointer();                     // will call function1

  FunctionPointer = function2;
  FunctionPointer();                     // will call function2

  ArrayOfFunctionPointers[0] = function1;
  ArrayOfFunctionPointers[1] = function2;

  for (int i = 0; i < 2; i++)
  {
    ArrayOfFunctionPointers[i]();
  }
}

Output: 输出:

function 1
function 2
function 1
function 2

First thing's first, the declaration you show is ill-formed. 首先,您显示的声明格式错误。 Not surprisingly, given how hard the declarator syntax in C can be. 考虑到C语言中的声明符语法有多难,这不足为奇。 If you wanted an array of function pointers (something I strongly suspect), then the raw spelling is like this: 如果您想要一个函数指针数组(我强烈怀疑),那么原始拼写是这样的:

int *(*functions_array[4])();

Not much more readable, but at least syntactically correct. 可读性不高,但至少在语法上正确。 You can now index into it to obtain a function pointer for your use. 现在,您可以对其进行索引以获得要使用的函数指针。 Eg: 例如:

functions_array[0] = function; 
functions_array[0](); // Call the function

But if you really wanted a pointer to an array (for reasons I can't fathom), the raw declarator is this: 但是,如果您真的想要一个指向数组的指针(出于我无法理解的原因),则原始声明符是这样的:

int *(*(*functions_array_p)[4])();

With the now less appealing indexing to match: 使用现在不那么吸引人的索引来匹配:

(*functions_array_p)[0] = function; 
(*functions_array_p)[0](); // Call the function

The parenthesis are required due to the regular precedence of * and [] . 由于*[]的规则优先级,因此需要括号。

But the way to make working with it and defining it easier, is by introducing some type aliases. 但是,使用它并使其更容易定义的方法是引入一些类型别名。 Primarily, for the function type: 首先,对于函数类型:

typedef int* function_type();

Now the array declaration takes this far more readable form: 现在,数组声明采用了更易读的形式:

function_type* functions_array[4];

The asterisk in plain sight is inline with the generally good practice of not hiding away pointer semantics. 显而易见的星号与不隐藏指针语义的一般良好做法是一致的。

And for a pointer to an array: 对于数组的指针:

function_type* (*functions_array)[4];

The declarator is still fairly regular looking. 声明符的外观仍然很规则。


1. As an aside, an empty parameter list in a C function declaration does not mean the function takes no arguments. 1.顺便说一句,C函数声明中的参数列表为空并不表示该函数不接受任何参数。 It's an obsolescent feature providing a function with no prototype. 这是一个过时的功能,提供了没有原型的功能。 The way forward is (void) . 前进的道路是(void)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM