Can I jump to a case inside a switch using goto?

Discussion in 'C Programming' started by rivkaumiller, Apr 3, 2014.

  1. gcc supports nested functions as an extension, which strike me as a
    cleaner way to do the same thing. The only difference would be that a
    goto can't (as far as I know) jump from one function to another, even if
    one is nested in the other, but I don't think that's a disadvantage.
     
    Keith Thompson, Apr 3, 2014
    #21
    1. Advertisements

  2. It can make debugging easier. You can print the value of the state
    variable on each iteration of the loop, either by setting a single
    breakpoint or by adding a single printf() call. If you want to trace
    every state transition in an FSM implemented with gotos, you have to add
    code to every state.
     
    Keith Thompson, Apr 3, 2014
    #22
    1. Advertisements

  3. Though C has permitted mixed declarations and statements since C99, it
    still doesn't permit labels on declarations. This:

    label: int i = 0;

    is a syntax error. (I just ran into this the other day.)

    You can just add a null statement:

    label:; int i = 0;
     
    Keith Thompson, Apr 3, 2014
    #23
  4. (snip on Algol-68 feature)
    Pascal and PL/I allow GOTO out of internal functions, but not in.
    In PL/I, you can do it with label variables.

    C has longjmp() if you really want to do it.

    -- glen
     
    glen herrmannsfeldt, Apr 3, 2014
    #24
  5. rivkaumiller

    James Kuyper Guest

    That's what I actually meant, though I didn't say it quite right.
     
    James Kuyper, Apr 3, 2014
    #25
  6. But that's just a funtion table, which is
    even more common in these types of languages...
     
    Lowell Gilbert, Apr 3, 2014
    #26
  7. rivkaumiller

    rivkaumiller Guest

    There is a phenomenal amount of context in this thread, could you just
    give a 10 sentence summary of the consensus - if one exists?
    Could you or someone else give a real example?
    Again, could you or someone else give a real example?
     
    rivkaumiller, Apr 3, 2014
    #27
  8. rivkaumiller

    rivkaumiller Guest

    There is a phenomenal amount of content in this thread, could you just give a 10 sentence summary of the consensus - if one exists?

    Could you reword and expand claims in your post below by real examples where I indicate a need for clarity?
    Could you or someone else give a real example?
    Again, could you or someone else give a real example?
     
    rivkaumiller, Apr 3, 2014
    #28
  9. rivkaumiller

    JohnF Guest

    Thanks for additional clarification, James (and for not
    pointing out how my i++ was pretty darned useless:).
    That seems to just emphasize the earlier judgement that
    this kind of branch is more than likely a bad thing.
     
    JohnF, Apr 4, 2014
    #29
  10. Yes, it looks like a function table but it's not quite the same as one.
    For one thing, the values don't actually derive from functions, and they
    also become invalid if the table is returned form the function that
    contains the labels.
     
    Ben Bacarisse, Apr 4, 2014
    #30
  11. There's two kinds of "computed" here: the index into a list of labels
    and the label list itself. In Fortran, the list was static --
    effectively a literal array -- and only the index is "computed". That
    kind of computed goto can be done in C:

    switch (J) { case 1: goto L1; case 2: goto L2; ... }

    It's clumsy to write, but the functionality is there (though I'm blessed
    never to have see it used!).

    In BCPL and in Algol 68 (and C with gcc's extensions) you can compute
    with the labels themselves, though there may be very severe
    restrictions on exactly what you can do. Once you can play with the
    value of a label in some way (however limited) you get more
    opportunities for "fun".
     
    Ben Bacarisse, Apr 4, 2014
    #31
  12. rivkaumiller

    Tim Rentsch Guest

    This analysis is right for C90 and C99. In C11, the access
    is i is simply undefined behavior, under 6.3.2.1 p2.
     
    Tim Rentsch, Apr 15, 2014
    #32
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.