3

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

0

3 Answers 3

10

You are very close...

Try:

void (*Event_Handlers[3])(); 
Sign up to request clarification or add additional context in comments.

5 Comments

thank you Nemo.. But still the warning persists.. wat may be the reason?
Seriously? What compiler? I tried your version (and got the warning) then mine (and it went away). I just tried it again to make sure... Try @Mihran's approach; it is clearer anyway.
My version is gcc. I tried Mihrans approach. Still i am getting that error
I assume the warning on line 10 is gone? Are you trying to put these assignments at the top level? You cannot have code outside of a function in C... You have to put them into a function (say, main())
@CHID: Have you tried using proper prototypes? void Queue(void) and void (*Event_Handlers[3])(void)? Your compiler could be slipping into K&R mode and getting confused. And yeah, Mihran's typedef (an awesome band name BTW) approach will be less confusing in the long run.
4

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];

3 Comments

thankk you Mihran. I tried ur code. But still my compiler complains.. Now i get a new error. "Conflictng types of EventHandlers followed by invalid initialiser". My code contains only the above mentioned lines. I have made sure that ther is no otherp ointer by the name EventHandlers
What compiler do you use? Are you sure there is no EventHandlers variable in other place in your code (try grep -r 'EventHandlers' ./ through all source files). Try to initialize EventHandlers after declaration (func_t EventHandlers[3] = {NULL}).
Thank you Mihran. The problem was solved. #Nemo's second comment
0

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

void (* Event_Handlers[3])()

will be the right answer

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.