在以下函数中,对于回调函数start_routine
,返回类型为void **
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
但是当我们定义回调函数时,它是这样的:
void func(void *)
我知道这是一个函数,但我认为至少回调函数应该像这样:
void* func(void *);
我哪里错了?谢谢!
void func(void *)
只是一个正常的函数,不返回任何值并采用void*
。它不能放在一个函数的参数。
void* func(void *)
与上面类似,唯一的区别是它返回一个void*
指针。
回调函数如下所示return_type (*function_name)(arguments)
。这里是一个示例void* (*start_routine)(void *)
。这是一个有效的函数参数。
该pthread_create
函数将指向该函数start_routine
的指针作为其倒数第二个参数,该函数的返回类型为void*
并且仅具有type的一个参数void*
。
此页面的摘录:http : //www.cprogramming.com/tutorial/function-pointers.html
有时候,当有更多的恒星投入时,人们会感到困惑:
void *(*foo)(int *);
在这里,关键是要由内而外阅读。请注意,表达式的最里面的元素是*foo
,否则它看起来像一个普通的函数声明。*foo
应该引用返回avoid*
并采用的函数int*
。因此,foo是仅指向此类函数的指针。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句