问题描述
我正在通过STM32 MCU上USB驱动程序的代码实现。我对C语言的理解有点受限制,我遇到了这个函数定义,但我不清楚。
static enum usbd_request_return_codes cdcacm_control_request(
usbd_device *usbd_dev __attribute__((unused)),struct usb_setup_data *req,uint8_t **buf __attribute__((unused)),uint16_t *len,void (**complete)
(
usbd_device *usbd_dev,struct usb_setup_data *req
) __attribute__((unused))
)
我不明白函数声明中的最后一个参数,好像它实际上是在为该参数定义另一个函数,并且使用奇怪的两个星号作为参数。有人可以解释一下这是什么以及在实际的函数调用中将如何使用它吗?
解决方法
此
void (**complete)
(
usbd_device *usbd_dev,struct usb_setup_data *req
) __attribute__((unused))
是指向函数指针的指针的声明。该函数具有返回类型void和两个参数。
为清楚起见,请考虑下面的演示程序。
#include <stdio.h>
void f( int x,int y )
{
printf( "x + y = %lld\n",( long long int )x + y );
}
void g( int x,int y,void ( **fp )( int,int ) )
{
( *fp )( x,y );
}
int main(void)
{
void ( *fp )( int,int ) = f;
g( 10,20,&fp );
return 0;
}
程序输出为
x + y = 30
在没有更广泛的上下文的情况下,很难说为什么使用指向函数的指针。也许是由于该参数是输出参数。或者可以动态分配指向函数的指针数组。