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

前端 未结 11 1536
说谎
说谎 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 17:08

    It is possible with GCC feature known as "labels as values".

    void *s[3] = {&&s0, &&s1, &&s2};
    
    if (n >= 0 && n <=2)
        goto *s[n];
    
    s0:
    ...
    s1:
    ...
    s2:
    ...
    

    It works only with GCC!

提交回复
热议问题