Pointers 回调函数和返回类型的声明
在以下函数中,对于回调函数Pointers 回调函数和返回类型的声明,pointers,callback,Pointers,Callback,在以下函数中,对于回调函数start\u例程,返回类型为void** int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 但是当我们定义回调函数时,它是这样的: void func(void *) 我知道这是一个函数,但我认为至少回调函数应该是这样的: void* func(void *);
start\u例程
,返回类型为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*
指针
回调函数如下所示返回类型(*function\u name)(参数)
。这里,一个例子是void*(*start\u例程)(void*)
。这是一个有效的函数参数
pthread\u create
函数将指向返回类型为void*
且只有一个类型为void*
的函数start\u例程的指针作为其第二个最后参数
本页摘录:
有时,当更多的星星被投进来时,人们会感到困惑:
在这里,关键是要从里到外阅读;请注意,表达式最里面的元素是*foo
,否则它看起来像一个正常的函数声明*foo
应指返回void*
并接受int*
的函数。因此,foo就是这样一个函数的指针。因此回调函数的定义应该像void*func(void*)
,但为什么在示例中,他们将其定义为void func(void*)
?不,回调函数仅定义为void*func(void*)
void func(void*)
将给出语法错误。参见thread\u start
:啊,他们这样称呼它:pthread\u create(&tinfo[tnum])。thread\u id,&attr,&thread\u start,&tinfo[tnum])
,在thread\u启动之前有一个&
,我想知道这个&
是否可以避免?不,追加&
给出了该函数的地址,分配给函数指针参数的。现在清楚了吗?我想函数名和它的地址是一样的,所以thread\u start等于&thread\u start,不是吗?
void *(*foo)(int *);