【问题标题】:how do I assign a function to an element of an array of function pointers如何将函数分配给函数指针数组的元素
【发布时间】:2016-02-19 16:51:06
【问题描述】:

在 Xcode 7.2.1 中,我声明了 3 个 C 函数

double function1(double);
double function2(double);
double function3(double);

现在我声明一个指向函数的指针并定义它。

double (*aFunctionPointer)(double)=function1;

没有错误,正如预期的那样。

现在我声明一个函数指针数组并用我的 3 个函数填充它。

double (*anArrayOfFunctionPointers[3])(double)={function1, function2, function3};

再一次,没有错误。

现在我定义了一个函数指针数组,但不要填充它。

double (*anotherArrayOfFunctionPointers[3])(double);

再一次,没有错误。

现在我尝试为其中一个数组元素分配一个函数。

anotherArrayOfFunctionPointers[1]=function2;

这一次,警告和错误:

  • 警告:缺少类型说明符,默认为 int
  • 错误:使用不同类型重新定义“anotherArrayOfFunctionPointers”:“int [0]”与“double (*[3]) (double)”

我被难住了。

背景是我正在尝试编写一个程序来在各种度量单位之间进行转换。我想我会使用一个包含函数指针的二维数组,以避免一些非常冗长的 switch 语句。

要进行转换,我会调用如下函数:

result=convert(someValueToConvert,yards,meters);

并且转换函数会从数组中调用正确的函数,如下所示:

return conversionFunctionArray[yards, meters](someValue);

数组会这样初始化:

conversionFunction[yards][meters]=yardsToMeters(somevalue);
conversionFunction[meters][yards]=metersToYards(somevalue);
conversionFunction[yards][feet]=yardsToFeet...
...

关于函数指针和数组,我缺少什么?

【问题讨论】:

  • typedef 是处理函数指针时非常有用的工具。尝试使用typedef double (*funcPtr_t)(double); 之类的方法来创建函数指针类型,然后尝试使用funcPtr_t funcPtrArray[3]; 之类的方法来创建数组。
  • 错误清楚地表明您已经重新定义 anotherArrayOfFunctionPointers。因此,在您的测试代码中的某处,您对该数组有一个较旧的定义。请注意,我逐行遵循了您的所有说明,没有收到任何错误或警告。这就是为什么该网站要求调试问题必须包含Minimal Complete Verifiable Example。

标签: c multidimensional-array function-pointers


【解决方案1】:

根据问题中的零碎说明,这是该问题的 Minimal Complete Verifiable Example 的样子。

#include <stdio.h>

double f1( double );
double f2( double );
double f3( double );

int main( void )
{
    // testing a single function pointer
    double (*ptr1)(double) = f1;
    printf( "%.0lf\n\n", (*ptr1)(5) );

    // testing an array of function pointers with an initializer list
    double (*array[3])(double) = { f1, f2, f3 };
    for ( int i = 0; i < 3; i++ )
        printf( "%.0lf\n", (*array[i])(6) );
    printf( "\n" );

    // testing an array of function pointers that is initialized by assignments
    double (*otherArray[3])(double);
    otherArray[0] = f1;
    otherArray[1] = f2;
    otherArray[2] = f3;
    for ( int i = 0; i < 3; i++ )
        printf( "%.0lf\n", (*otherArray[i])(7) );
    printf( "\n" );
}

double f1( double x ) { return 10+x; }
double f2( double x ) { return 20+x; }
double f3( double x ) { return 30+x; }

请注意,此代码编译时没有任何错误或警告,并产生预期的输出。再次说明为什么调试问题必须包含Minimal Complete Verifiable Example。

【讨论】:

    【解决方案2】:

    一个明显的问题是您实际上没有定义任何函数function1 等。

    要看到这一点,让我们只使用 one 函数和仅 one 元素的数组。把它放在顶层:

    double function1(double);
    double (*aFunctionPointer)(double)=function1;
    double (*anArrayOfFunctionPointers[1])(double)={function1};
    double (*anotherArrayOfFunctionPointers[1])(double);
    

    在实际的可执行代码中,这样做:

    anotherArrayOfFunctionPointers[0]=function1; // error
    

    这是一个错误。但现在让我们实际定义 function1,像这样:

    double function1(double f) {return 3.0;};
    double (*aFunctionPointer)(double)=function1;
    double (*anArrayOfFunctionPointers[1])(double)={function1};
    double (*anotherArrayOfFunctionPointers[1])(double);
    

    现在让我们试试同样的代码:

    anotherArrayOfFunctionPointers[0]=function1;
    

    没有错误。不同之处在于,现在我们实际上拥有一个function1 指向。有了你的代码,那里就没有了。

    【讨论】:

    • 顺便说一下,在风格上,当您传递函数指针时,您可能希望使用与符号:&amp;function1。这绝对没有必要,但我发现它有助于明确意图。
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2016-12-03
    • 2013-01-16
    相关资源
    最近更新 更多