0

I have three function arrays each pointing to a number of functions.

I can call any of those functions form the three tables.

Now I would like to dereference the three arrays into a single array of function pointers but I just can't get it working!

void afunc1(void);
void afunc2(void);
void afunc3(void);
void bfunc1(void);
void bfunc2(void);
void bfunc3(void);
void cfunc1(void);
void cfunc2(void);
void cfunc3(void);

void(*FuncTbla[])(void) = { afunc1, afunc2, afunc3 };
void(*FuncTblb[])(void) = { bfunc1, bfunc2, bfunc3 };
void(*FuncTblc[])(void) = { cfunc1, cfunc2, cfunc3 };

void (*AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc };

int TblNo = 1, FuncNo = 1; // tblNo 1 = table b

bFunc2(); // calls bFunc2 directly

FuncTblb[FuncNo](); // Calls Function bFunc2 via function table b

// Call same function using table of function tables
AllFuncTbls[TblNo][FuncNo](); // Does not compile - expression must be a pointer to a complete object type!!!

3 Answers 3

4

Two things: First of all remember that arrays naturally decays to pointers to their first element; And secondly it will become so much easier if you use type-aliases for the function types.

Armed with that knowledge you could do it like e.g.

// Type-alias to simplify using function pointers
typedef void (*function_type)(void);

// The three tables
function_type FuncTbla[] = { &afunc1, &afunc2, &afunc3 };
function_type FuncTblb[] = { &bfunc1, &bfunc2, &bfunc3 };
function_type FuncTblc[] = { &cfunc1, &cfunc2, &cfunc3 };

// A table of pointers to the first elements of each array
function_type *AllFuncTbls[] = { FuncTbla, FuncTblb, FuncTblc };

To call a function using AllFuncTbls is as simple as

AllFuncTbls[TblNo][FuncNo]();
Sign up to request clarification or add additional context in comments.

3 Comments

That didn't work! The assignment works fine but the using it to call the function doesn't compile!
Refused to compile with fore mentioned error!!! What did work was changing void (*AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc }; to void (**AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc }; All good now!
@Walter So you didn't really do what I wrote in my answer? Which might explain why it didn't work, until you made some other changes that were also not part of my answer. Please, next time you say an answer doesn't work then you need to tell the poster what changes you made, and what parts of the answer you didn't follow or deviated from.
2

If you use typedefs it works:

void afunc1(void);
// ...

typedef void (*funcPtr)(void);
// void(*FuncTbla[])(void) = { afunc1, afunc2, afunc3 };
// ...
funcPtr FuncTbla[] = { afunc1, afunc2, afunc3 };
funcPtr FuncTblb[] = { bfunc1, bfunc2, bfunc3 };
funcPtr FuncTblc[] = { cfunc1, cfunc2, cfunc3 };

//void (*AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc };
funcPtr* AllFuncTbls[] = { FuncTbla, FuncTblb, FuncTblc };
// Use an Array of pointers to function pointers here, not an array of function pointers!

// ...    

// Call same function using table of function tables
AllFuncTbls[TblNo][FuncNo](); // Compiles now

I commented out the lines that had to be changed.

Comments

1

Using typealiases is better approach, but if you were courious how to do it without it:

void(**AllFuncTbls[])(void) = { FuncTbla, FuncTblb, FuncTblc};

Comments

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.