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

前端 未结 11 1649
说谎
说谎 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条回答
  •  情书的邮戳
    2020-12-28 16:55

    There's no direct way to store code addresses to jump to in C. How about using switch.

    #define jump(x)  do{ label=x; goto jump_target; }while(0)
    int label=START;
    jump_target:
    switch(label)
    {
        case START:
            /* ... */
        case LABEL_A:
            /* ... */
    }
    

    You can find similar code produced by every stack-less parser / state machine generator. Such code is not easy to follow so unless it is generated code or your problem is most easily described by state machine I would recommend not do this.

提交回复
热议问题