如何将函数分配给函数指针数组的元素
how do I assign a function to an element of an array of function pointers
在Xcode7.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...
...
关于函数指针和数组,我遗漏了什么?
一个明显的问题是您实际上没有定义任何函数 function1
等
为了看到这一点,让我们仅使用 one 函数和仅包含 one 元素的数组来执行此操作。将其置于顶层:
double function1(double);
double (*aFunctionPointer)(double)=function1;
double (*anArrayOfFunctionPointers[1])(double)={function1};
double (*anotherArrayOfFunctionPointers[1])(double);
在实际的可执行代码中,这样做:
anotherArrayOfFunctionPointers[0]=function1; // error
这是一个错误。但是现在让我们实际 define 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
指向。 你的代码,那里没有。
根据问题中的零碎说明,此问题的 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.
在Xcode7.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...
...
关于函数指针和数组,我遗漏了什么?
一个明显的问题是您实际上没有定义任何函数 function1
等
为了看到这一点,让我们仅使用 one 函数和仅包含 one 元素的数组来执行此操作。将其置于顶层:
double function1(double);
double (*aFunctionPointer)(double)=function1;
double (*anArrayOfFunctionPointers[1])(double)={function1};
double (*anotherArrayOfFunctionPointers[1])(double);
在实际的可执行代码中,这样做:
anotherArrayOfFunctionPointers[0]=function1; // error
这是一个错误。但是现在让我们实际 define 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
指向。 你的代码,那里没有。
根据问题中的零碎说明,此问题的 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.