Search code examples
c++arraysfunction-pointersdeclaration

How to declare an array of functions?


Suppose I want to declare an array of functions, and try the following

int (*funcs)(int, int)[10];

But turns out that the following declaration stands for an function returning an array, which does not compile.

How can I declare an array of functions properly?


Solution

  • Strictly speaking you may not declare an array of functions. You may declare an array of function pointers.

    It seems you mean

    int (*funcs[10])(int, int);
    

    Another way is to introduce a using declaration (or a typedef declaration) like for example

    using FP = int( * )( int, int );
    
    FP funcs[10];
    

    or

    using FUNC = int ( int, int );
    
    FUNC * funcs[10];