I would like to display the output - numbers 1 to 5, followed by 4-5 infinitely. Is there any way i can pass the value of i(4) instead of the character i in goto1. Or is there any other efficient way of realizing this without illustrating all the options as in switch(i.e case 1: goto1(c1) ,etc..).
The main aim is to jump to a statement whose label is computed within the program.
#define goto1(i) \
goto c##i
int main(){
c1 : printf(" num is 1 \n");
c2 : printf(" num is 2 \n");
c3 : printf(" num is 3 \n");
c4 : printf(" num is 4 \n");
c5 : printf(" num is 5 \n");
int i=4;
goto1(i);
}
Are you asking for a jump table? If you are using gcc: It has a jump table mechanism.
The pro about this method is that you spare the large switch statement.
If you are ... adventurous (or do I mean silly?), you can use a GCC extension Labels as Values.
Under no circumstances should this be taken as a recommendation to use the feature. The computed
goto
was eventually removed from Fortran; it is best left in the dustbin of history.Since you want to do this the wrong (aka. creative) way, have you considered trampolining?
Continuing on from the idea of using function pointers (see what I did there? Giggity!), you could use an array of function pointers:
Why not do it like this?
Wouldn't a switch accomplish the same thing?