[英]Array of function pointers in C
I'm having really hard time comprehending the syntax for function pointers. 我很难理解函数指针的语法。 What I am trying to do is, have an array of function pointers, that takes no arguments, and returns a void pointer.
我要做的是,有一个函数指针数组,不带参数,并返回一个void指针。 Can anyone help with that?
任何人都可以帮忙吗?
First off, you should learn about cdecl
: 首先,您应该了解
cdecl
:
cdecl> declare a as array 10 of pointer to function(void) returning pointer to void void *(*a[10])(void )
You can do it by hand - just build it up from the inside: 你可以手工完成 - 只需从内部构建它:
a
is an array: 是一个数组:
a[10]
of pointers: 指针:
*a[10]
to functions: 功能:
(*a[10])
taking no arguments: 不参数:
(*a[10])(void)
returning void *
: 返回
void *
:
void *(*a[10])(void)
It's much better if you use typedef
to make your life easier: 如果你使用
typedef
让你的生活更轻松,那就更好了:
typedef void *(*func)(void);
And then make your array: 然后制作你的数组:
func a[10];
Whenever compound syntax gets too complicated, a typedef usually clears things up. 每当复合语法过于复杂时,typedef通常都会清除。
Eg 例如
typedef void *(* funcPtr)(void);
funcPtr array[100];
Which without the typedef I guess would look like: 没有typedef,我猜想会是这样的:
void *(* array[100])(void);
Start with the array name and work your way out, remembering that []
and ()
bind before *
( *a[]
is an array of pointer, (*a)[]
is a pointer to an array, *f()
is a function returning a pointer, (*f)()
is a pointer to a function): 从数组名称开始,然后解决问题,在
*
( *a[]
是指针数组之前记住[]
和()
绑定, (*a)[]
是指向数组的指针, *f()
是返回指针的函数, (*f)()
是指向函数的指针):
farr -- farr
farr[N] -- is an N-element array
*farr[N] -- of pointers
(*farr[N])( ) -- to functions
(*farr[N])(void) -- taking no arguments
*(*farr[N])(void) -- and returning pointers
void *(*farr[N])(void); -- to void
Use typedef
s 使用
typedef
typedef void* func(void);
func *arr[37];
查看http://www.newty.de/fpt/fpt.html#arrays ,了解C和C ++函数指针数组的示例和解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.