Long day! Been up for 46 hours don’t ask, not pretty, and I’m very pissed off! So what’s all this about, I was writing some code, that was proclaimed finished three weeks ago, to find out that, well, it needs to be ported to a new hardware, and the demo is today (I was told this last night). I’ve been working on unrelated critical project, which has made a mess of sleep cycle.
One of the beauty of said hardware spec, is that there is a set of instructions that needs to be run in sequential order, checked, and continue to the next instruction. Now each instruction is a separate function, because they are huge and are reused in other parts of the code. The spec had a list of all the functions names (of course the list was in a spread sheet) so I just copied and pasted the functions name, to the code (I don’t do copy and paste, but after only having slept for an hour, It looked might attractive).
So in my laziness I came up with this:
1 void *(*p_foo[])() = ; 6 7 for (i = 0; i < ILIMIT; i++) 8 p_foo[i](param1,param2));
Now this works, because all the functions share the same prototype, and return a pointer to void. The value of ILIMIT is huge, so I saved myself a lot of typing. I don’t remember seeing that idiom anywhere, I’m not claiming I made it up, or is my invention or anything of the sort. It’s not that creative to begin with, and can be made a lot better.
But if you ever find yourself with a similar problem, and don’t want to write code to generate the function calls for you (or are to tired to do so :)). This is a clean alternative.
Going to sleep now.
Post a Comment