2

I'm using a function pointer based state machine. I need a function to check for different stats. The amount of functions is pretty massive. Therefore I would like to not use if else if else... chains. I would like to use a swithc case statement: I used the following code:

    switch (my_state_func)
    {
       case &idle_state_func:
       case NULL:
          //...
          break;
       case &failed_func:
          break;
       default:
          break;
    }

Now the compiler tells me I can't use pointer in switch case

Is there a way to fix this? I tried integer casting of the pointer. This generates the error:

label doesn't reduce to an integer constant

case (int)&idle_func:

Is there maybe a macro or somthing? Logically it should be possible since every function point is a integer.

Thanks for help.

Schafwolle
  • 501
  • 3
  • 15
  • 9
    Isn't the point of function pointers that you can just call them without a `switch`? This looks a bit like a design problem. – M Oehm Apr 27 '16 at 07:48
  • case-labels need to be `int` literals. `(int)&idel_func` isn't one. – alk Apr 27 '16 at 07:51
  • Another duplicate here: http://stackoverflow.com/q/2308323/694576 – alk Apr 27 '16 at 07:52
  • Yes i could use a additional status flag. Then i need to change the flag in every state to indecate errors and other stuff. I personally think a nicer solution is the usage of a dummy function to identicat an error state. This would fit more into the logical construt of a state machine – Schafwolle Apr 27 '16 at 08:01

0 Answers0