简体   繁体   English

将参数传递给函数指针数组

[英]Passing argument to array of function pointers

I am writing a program in C which uses an array of function pointers. 我正在用C编写一个使用函数指针数组的程序。 If I do not include an argument I am able to call the functions with my code without an issue. 如果不包含参数,则可以使用代码调用函数而不会出现问题。

int (*functionsArray[2][3])() = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

However when I try and pass the argument int x : 但是,当我尝试传递参数int x

int (*functionsArray[2][3])(int x) = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

I get an error: 我收到一个错误:

invalid conversion from 'int (*)()' to 'int (*)(int)'

Also none of these functions return an int, shouldn't I be able to declare them as void? 同样,这些函数都不返回int,我不应该将它们声明为void吗?

void(*functionsArray[2][3])(int x) = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

Trying this results in an error: 尝试此操作将导致错误:

 error: invalid conversion from 'int (*)()' to 'void (*)(int)'

Thanks. 谢谢。

That will work fine, provided you declare the functions correctly: 只要您正确声明函数,该方法就可以正常工作:

#include <stdio.h>

int functionOne(int x)   { return 1; }
int functionTwo(int x)   { return 2; }
int functionThree(int x) { return 3; }
int functionFour(int x)  { return 4; }
int functionFive(int x)  { return 5; }
int functionSix(int x)   { return 6; }

int (*functionsArray[2][3])(int x) = {
    {functionOne,  functionTwo,  functionThree},
    {functionFour, functionFive, functionSix}
};

int main (void) {
    printf ("%d\n", (functionsArray[0][1])(99));
    printf ("%d\n", (functionsArray[1][2])(99));
    return 0;
}

The output of that program is 2 and 6 . 该程序的输出为26


It will also work if you want no return value: 如果您不希望返回任何值,它也将起作用:

#include <stdio.h>

void functionOne(int x)   { puts ("1"); }
void functionTwo(int x)   { puts ("2"); }
void functionThree(int x) { puts ("3"); }
void functionFour(int x)  { puts ("4"); }
void functionFive(int x)  { puts ("5"); }
void functionSix(int x)   { puts ("6"); }

void (*functionsArray[2][3])(int x) = {
    {functionOne,  functionTwo,  functionThree},
    {functionFour, functionFive, functionSix}
};

int main (void) {
    (functionsArray[0][1])(99);
    (functionsArray[1][2])(99);
    return 0;
}

That program also outputs 2 and 6 , as expected. 该程序还会输出26 ,这与预期的一样。


It all comes down to ensuring that the function declarations match the type given in the array. 归结为确保函数声明与数组中给定的类型匹配。

When you declare an array: 声明数组时:

int (*functionsArray[2][3])(int x) = 
{
   {functionOne,functionTwo,functionThree},
   {functionFour,functionFive,functionSix}
};

every element of the array has to be of type int (*)(int) . 数组的每个元素都必须为int (*)(int) Otherwise, the compiler correctly reports an error. 否则,编译器会正确报告错误。

Take a simple case: 举一个简单的例子:

void foo()
{
}

int (*fp)(int x) = foo;

should result in the same compiler error because you are trying to initialize a variable of type int (*)(int) using foo , whose type is void (*)() . 应该会导致相同的编译器错误,因为您尝试使用foo初始化类型为int (*)(int)的变量,其类型为void (*)()

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

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