4 ms·
Presumably C's `switch` is also a problem.
by RossBencina 2y ago
Presumably C's `switch` is also a problem.
- metadat 2y agoYes, I don't recall all the confusing elements and technicalities of what's allowed in Switch statements in C offhand but here are a few brainfscks: https://old.reddit.com/r/C_Programming/comments/16kg48y/mind_blown_by_switch_statement_syntax_and_labels/ https://old.reddit.com/r/C_Programming/comments/16kg48y/mind... https://old.reddit.com/r/programminghorror/comments/ylc7f3/why_do_c_devs_love_switch_statements_so_much/ https://old.reddit.com/r/programminghorror/comments/ylc7f3/w...
- r1chardnl 2y agoI went down a rabbithole and wow. Found a comment from the author of https://github.com/stclib/STC https://github.com/stclib/STC apparently and then came across this example: https://stackoverflow.com/a/76887723 https://stackoverflow.com/a/76887723 int coro_a(struct a* g) { cco_routine (g) { printf("entering a\n"); for (g->i = 0; g->i < 3; g->i++) { printf("A step %d\n", g->i); cco_yield(); } cco_final: printf("returning from a\n"); } return 0; // done } gcc -E -ISTC/include co.c After running it through a preprocessor, it gives me this. int coro_a(struct a* g) { for (int* _state = &(g)->cco_state; *_state != CCO_STATE_DONE; *_state = CCO_STATE_DONE) _resume: switch (*_state) case 0: { printf("entering a\n"); for (g->i = 0; g->i < 3; g->i++) { printf("A step %d\n", g->i); do { *_state = 14; return CCO_YIELD; goto _resume; case 14:; } while (0); } *_state = CCO_STATE_FINAL; case CCO_STATE_FINAL: printf("returning from a\n"); } return 0; }
- mananaysiempre 2y agoI don’t want to become the switch-statement guy, but neither can I resist, apparently. There are no technicalities in what is allowed in a switch statement: the same things are as with bare gotos. That is, a switch statement is a fancy goto, and case labels are just labels that look a bit funny. Except for the case labels being restricted to inside of the switch body, nesting doesn’t really come into it. So then the question becomes, which things are you allowed to jump over? In C++, I don’t really know, the restrictions seem fairly stringent. In C, you can jump over anything except a declaration using a variably modified type (i.e. a variable-length array, a pointer to one, etc.), but keep in mind that the variables whose declarations you’ve jumped over will be uninitialized even if the declaration does have an initializer.