CHID
CHID

Reputation: 1643

array of function declaration and assignment problem

I am trying to create an array of pointers to functions, and trying to assign the function address to it. But i am getting errors which i dont know how to proceed with.

My code

void (*Event_Handlers)[3]();  //Line no 10

/* Queue, Drop and Send_back have been defined. for eg */
void Queue()
{
....
}

Event_Handlers[0]=Queue;  // Line 35
Event_Handlers[1]=Drop;   // Line 36
Event_Handlers[2]=Send_Back;   // Line 37

But i am getting the following error

 fsm.c:10: error: declaration of âEvent_Handlersâ as array of functions

 fsm.c:35: warning: data definition has no type or storage class

 fsm.c:35: error: invalid initializer

 fsm.c:36: warning: data definition has no type or storage class

 fsm.c:36: error: invalid initializer

 fsm.c:37: warning: data definition has no type or storage class

 fsm.c:37: error: invalid initializer

Where am in going wrong

Upvotes: 3

Views: 8240

Answers (3)

DinoStray
DinoStray

Reputation: 774

the array element type could be function pointer, but can not be function.

void (* Event_Handlers[3])()

will be the right answer

Upvotes: 0

Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11088

For first time to be sure that you declared array of pointers to function use the following syntax:

typedef void (*func_t)();
func_t EventHandlers[3];

Upvotes: 4

Nemo
Nemo

Reputation: 71525

You are very close...

Try:

void (*Event_Handlers[3])(); 

Upvotes: 10

Related Questions