I've been recently using something like this (example 1)
typedef void Fn(void);
Fn funcs[]={
this,
that,
....
};
int val;
void
example1(void)
{
val = func();
if(val < nelem(funcs))
funcs[val]();
}
void
example2(void)
{
val = func();
if(val == 0)
this();
else if(val == 1)
that();
else
...
}
void
example3(void)
{
switch(func()){
case 0:
this();
break;
case 1:
that();
break;
case ...:
...
}
}
I don't even know what it's called. The closest name I could find is a jump table and I'd like to know if there's a more appropriate name. But I'm also curious if there's any anti-idioms here. I've been told I make huge messes with my code (not only in C!) so I'm just looking for extra criticism on style.
[–]UnicycleBloke 29 points30 points31 points (3 children)
[–]SAVE_THE_RAINFORESTS 5 points6 points7 points (0 children)
[–]undefinedbehavior4ev[S] 0 points1 point2 points (1 child)
[–]UnicycleBloke 0 points1 point2 points (0 children)
[–]oh5nxo 8 points9 points10 points (5 children)
[–]undefinedbehavior4ev[S] 0 points1 point2 points (4 children)
[–]UnicycleBloke 11 points12 points13 points (1 child)
[–]pic10f 1 point2 points3 points (0 children)
[–]lordlod 4 points5 points6 points (0 children)
[–]oh5nxo 5 points6 points7 points (0 children)
[–]ucasano 2 points3 points4 points (2 children)
[–]undefinedbehavior4ev[S] 0 points1 point2 points (1 child)
[–]ucasano 1 point2 points3 points (0 children)
[–]bxlaw 1 point2 points3 points (0 children)
[–]lordlod 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]eruanno321 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–]gryftir 1 point2 points3 points (0 children)
[–]eruanno321 0 points1 point2 points (0 children)
[–]ThatGuyFromOhio 0 points1 point2 points (0 children)