Reputation: 1748
int kpSize = 4;
int kpIdx = 0;
typedef int (*EventHandler) (void *);
EventHandler *keyFuncArray = (EventHandler *) malloc(sizeof(EventHandler) * kpSize);
I get the following error on compiling, error C2099: initializer is not a constant on the following line
EventHandler *keyFuncArray = (EventHandler *) malloc(sizeof(EventHandler) * kpSize);
Upvotes: 0
Views: 317
Reputation: 1395
You can only do declarations in the global scope. So a function call can't execute in the global scope. As malloc()
is also a function call, it couldn't execute.
So you can declare the global pointer variable and initialize it in any of your functions (not restricted to main
only). Since the pointer is global it is available globally after initialization to any of your functions.
Upvotes: 2
Reputation: 16223
You cannot init with malloc
a global variable.
You must write, eg:
EventHandler *keyFuncArray;
int main ()
{
keyFuncArray = malloc(sizeof(EventHandler) * kpSize)
// STUFF:..
return 0;
}
Take a look also to: Do I cast malloc return?
Upvotes: 3