Is it possible to store the address of a label in a variable and use goto to jump to it?

前端 未结 14 1606
执念已碎
执念已碎 2020-12-04 09:35

I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I\'m not looking for \"don\'t do that\

14条回答
  •  情歌与酒
    2020-12-04 10:34

    You can do something like Fortran's computer goto with pointers to functions.

    // global variables up here
    
    void c1(){ // chunk of code
    
    }
    
    void c2(){ // chunk of code
    
    }
    
    void c3(){
    // chunk of code
    
    }
    
    void (*goTo[3])(void) = {c1, c2, c3};
    
    // then
    int x = 0;
    
    goTo[x++] ();
    
    goTo[x++] ();
    
    goTo[x++] ();
    

提交回复
热议问题