4

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.

5
  • Its nearly the former option - the array is static rather than global. cdecl is a handy way of checking this sort of thing out. Commented Aug 21, 2013 at 21:36
  • @simonc Well can I use this array in any of the classes IN the header file? Commented Aug 21, 2013 at 21:37
  • 1
    see this SO question, it may give some insights.. Commented Aug 21, 2013 at 21:38
  • @ShimonRachlenko nothing about static arrays there Commented Aug 21, 2013 at 21:39
  • @SusanYanders If you want to declare this in a header, you'll have to either declare it in a class or mark it as extern Commented Aug 21, 2013 at 21:40

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Can I declare this in a header file? Outside of a class?
@SusanYanders: You can declare that in almost any place you could declare a variable; the thing is, what are you trying to accomplish?
Either static in .cpp or extern in .h
How is introducing an extraneous typedef "as simple as possible"?
yeah i wouldnt use a typedef i can read it easier with plain old int (*myArray[5])();
|
3

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.

2 Comments

"Without the static myArray is global to the file" - It's actually global to the program, the rest of the program just does not know about it. With static on the other hand it is "global" to the file (meaning accessible from any function).
Thanks - I changed the wording a bit to make my intent clearer (I hope).
1

static is a storage class specifier, not a type specifier or qualifier, so it specifies the storage class of the variable and has no effect on its type. There's no such thing as a 'static' type or a 'function returning static' -- only static variables and static functions/methods.

Comments

1

The following link: //www.cdecl.org/ is helpful (in fact I have it bookmarked). For your definition it says:

declare myArray as static array 5 of pointer to function returning int

Notice that the description correctly classifies the array of 5 pointer as static and the function just returns an int.

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.