I need a global array of function pointers, and came up with this:
static int (*myArray[5])();
if i am right, this is "a global array of pointers to functions returning int". Is that right? Or is it "an array of pointers to functions returning a static int". I just need a quick answer.
Should be made as simple as possible, but not simpler:
typedef int (*t_MyFunc)();
t_MyFunc g_MyFuncArray[5];
And g_MyFuncArray
can be static if you wish (but you should not if you want a global variable):
static t_MyFunc g_MyFuncArray[5];
In a header file you should write:
extern t_MyFunc g_MyFuncArray[5];
But don't forget to omit the static
keyword in a .cpp
file in this case.
You don't want or need the static keyword for a global variable. In a .cpp file using static will give the variable internal linkage (you want external linkage). Without the static, myArray is functionally global only to the file. If you want it visible to your entire program you add
extern int (*myArray[MY_FUNC_ARRAY_SIZE])();
to your .h file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With