Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Can anyone help with that?

like image 925
yasar Avatar asked Mar 12 '12 18:03

yasar


1 Answers

  1. 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 )
    
  2. 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 *(*a[10])(void)

  3. It's much better if you use typedef to make your life easier:

    typedef void *(*func)(void);
    

    And then make your array:

    func a[10];
    
like image 79
Carl Norum Avatar answered Oct 02 '22 11:10

Carl Norum