看懂这个
typedef long BaseType_t;
typedef BaseType_t (*TaskHookFunction_t)( void * );
TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask ) ;
对照
void FunB(int n)
{
printf("call FunB. n is : %d\n", n);
}
typedef void (*PtrFunB)(int);
void usePtrFunB(PtrFunB p, int n)
{
p(n);
}
PtrFunB getPtrFunB()
{
PtrFunB p = FunB;
return p;
}
函数指针作为其它函数的参数,特别是作为返回值时,直接使用的函数指针无法编译。
下面这种语法编译器已经无法识别了
(void (*PtrFunB)(int)) getPtrFunA2() {
return FunB;
}
int add(int a, int b) {
return a + b;
}
typedef int (PTypeFun1)(int, int);
typedef int (*PTypeFun2)(int, int);
int (*padd)(int, int);
int main() {
PTypeFun1 *pTypeAdd1 = add;
PTypeFun2 pTypeAdd2 = add;
padd = add;
cout << pTypeAdd1(1, 2) << endl;
cout << pTypeAdd2(1, 2) << endl;
cout << padd(1, 2) << endl;
return 0;
}
参考这篇文章:
使用 typedef 定义函数指针,更优雅的使用函数指针
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)