6
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?

3 Answers 3

21

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. 
Sign up to request clarification or add additional context in comments.

Comments

8

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".)

3 Comments

This compiled, I will have to get back to you as to whether it worked!
@jliu83: Haha okay! (It probably did. :P)
The right way to verify that you declared the pointer correctly is to assign the address of the array to it, i.e. ptr_to_fixed_length_function_array = &array_of_functions;. If the type isn't correct, your compiler should complain about this assignment.
5

Try this:

typedef int(*rdPtrList_t[4])(unsigned int addr, unsigned int data);
rdPtrList_t *ptrToArray;

2 Comments

This worked! What would be the syntax for not using typedef? Is it even possible without the typedef?
@Idanko: Both solutions worked. However I prefer the not to use the typedef. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.