How to store goto labels in an array and then jump to them?

前端 未结 11 1608
说谎
说谎 2020-12-28 16:20

I want to declare an array of \"jumplabels\".

Then I want to jump to a \"jumplabel\" in this array.

But I have not any idea how to do this.

It should

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 17:10

    goto needs a compile-time label.

    From this example it seems that you are implementing some kind of state machine. Most commonly they are implemented as a switch-case construct:

    while (!finished) switch (state) {
      case s0:
      /* ... */
      state = newstate;
      break;
    
      /* ... */
    }
    

    If you need it to be more dynamic, use an array of function pointers.

提交回复
热议问题