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

前端 未结 14 1613
执念已碎
执念已碎 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:24

    You can do something similar with setjmp/longjmp.

    int main (void)
    {
        jmp_buf buf;
        int i=1;
    
        // this acts sort of like a dynamic label
        setjmp(buf);
    
        if( i-- )
            // and this effectively does a goto to the dynamic label
            longjmp(buf, 1);
    
        return 0;
    }
    

提交回复
热议问题