Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to an array of function pointers

int (*rdPtrList[4])(unsigned int addr, unsigned int data);

The above declares an array of size 4 of pointers for functions that returns an int and takes two unsigned int. I would like to make a pointer to this array. Is this possible in C?

like image 308
jliu83 Avatar asked Apr 06 '11 21:04

jliu83


2 Answers

Leaving out the parameters to keep the following easier to read:

        p         -- p
       *p         -- is a pointer
      (*p)[4]     -- to a 4-element array
     *(*p)[4]     -- of pointers
    (*(*p)[4])()  -- to functions 
int (*(*p)[4])(); -- returning int. 
like image 129
John Bode Avatar answered Oct 04 '22 21:10

John Bode


Ah, tricky tricky!!!

I think this works

int (*(*rdPtrList)[4])(unsigned int addr, unsigned int data);

because the compiler tells me _countof(*rdPtrList) is 4.


(I wish you could just say int function(unsigned int addr, unsigned int data)[4]* like you can in D, it's so much more readable: it would be a "function array pointer".)

like image 25
user541686 Avatar answered Oct 04 '22 22:10

user541686