newhand said:
If somehow a bunch of function names in string can be passed into an
executible, is it possible that for each name call, it will trigger the
corresponding function of that name? Of course, this can be done by mapping
string function names with functions themselves?
If you're refering to searching through an array/list of strings of
function name to find the corresponding function pointer, then yes
that's the standard wrok-around.
Is there a better and direct way?
No.
Actually, you can in theory get the addresses of your
functions/subroutine (at assembly & machine code level functions are
really just subroutines). But only if your executable is compiled with
debugging symbols (-g option for gnu compilers). That's how debuggers
work. Remember that at machine code level the function names don't
exist. They are just addresses in memory and machines tend to have
numbers as addresses ;-)
Anyway, you really don't want this as this prevents you form generating
optimised executales on most compilers.
One common design practice in embedded systems is to call functions by
numbers. Protocols, commands and datatypes tend to be a one-byte field
in packets. So you would use an array of 256 function pointers with
each index in the array pointing to the appropriate packet handler for
a given command. It's also very fast since indexing an array is much
faster than both searching strings or if-else statements.