0

We have a label:

LABEL:
    //Do something.

and we have a function. We want pass LABEL as a argument to this function (otherwise we can't access labels in a function), and in some condition we want to jump this label. Is it possible?

I'm giving a example (pseudocode) for clarification:

GameMenu: //This part will be executed when program runs
//Go in a loop and continue until user press to [ENTER] key

while(Game.running) //Main loop for game
{
    Game.setKey(GameMenu, [ESCAPE]); //If user press to [ESCAPE] jump into GameMenu
    //And some other stuff for game
}    
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Ibrahim Ipek
  • 479
  • 4
  • 13
  • 3
    You can't jump to a label outside of the current function. – flau Aug 04 '16 at 12:55
  • @iwin: We don't know if that "C/C++" language does allow this. Apparently OP thinks C and C++ are the same language. They are **not** – too honest for this site Aug 04 '16 at 13:15
  • @Olaf I was thinking of goto which wouldn't let you jump to an external label in either language, didn't know about setjmp / longjmp. – flau Aug 04 '16 at 13:18
  • @iwin: Well, it works in BASIC and Assembler for instance. Without proper context, there is no really good answer, but only speculation. VTC as unclear. `goto` has its uses, but chances are quite good OP's is none of them. – too honest for this site Aug 04 '16 at 13:26

3 Answers3

5

This sounds like an XY problem. You might want a state machine:

enum class State {
    menu,
    combat,
};

auto state = State::combat;
while (Game.running) {
    switch (state) {
    case State::combat:
        // Detect that Escape has been pressed (open menu).
        state = State::menu;
        break;

    case State::menu:
        // Detect that Escape has been pressed (close menu).
        state = State::combat;
        break;
    }
}
Simple
  • 13,992
  • 2
  • 47
  • 47
1

It seems like it may be worth refactoring your code to something similar:

void GameMenu() {
    // Show menu
}

void SomethingElse() {
    // Do something else
}

int main(int argc, char **argv) {

    (...)

    while(Game.running) {
        int key = GetKey();
        switch(key) {
        case ESCAPE:
            GameMenu();
            break;
        case OTHER_KEY:
            SomethingElse();
            break;
        }
    }
}
flau
  • 1,328
  • 1
  • 20
  • 36
-1

You can use setjmp()/longjmp() to jump to some point in outer scope even to outer function. But note - jump target scope must be alive at the moment of jump.

Sergio
  • 8,099
  • 2
  • 26
  • 52