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. Can anyone help with that?
-
6You'll generally get a better response if you post some code that you tried, even if it is not working, and explain as best you can exactly what the problem is. It demonstrates more effort on your part.Eric J.– Eric J.2012-03-12 18:32:41 +00:00Commented Mar 12, 2012 at 18:32
-
check this linkVikram– Vikram2012-03-12 19:04:15 +00:00Commented Mar 12, 2012 at 19:04
5 Answers
First off, you should learn about
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:
ais an array:
a[10]of pointers:
*a[10]to functions:
(*a[10])taking no arguments:
(*a[10])(void)returning
void *:void *(*a[10])(void)It's much better if you use
typedefto make your life easier:typedef void *(*func)(void);And then make your array:
func a[10];
1 Comment
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):
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
Comments
Check out http://www.newty.de/fpt/fpt.html#arrays for examples and explainations of arrays of C and C++ function pointers.