Calling a bound function

Viewed 374

I'm trying to achieve something I've easily done in dynamic languages, but have no idea how to do in C.

I have a program that needs to call different functions depending on a given string by the user. Each function has a C file of its own.

The easy way to do this is just do if-else-elseif along with strcmp and call each function if it matches.

I want do to something nicer: at the end of each C file holding each significant function, add a "binding" call, a bit like this:

int my_action(char smth[]);

bind_function("myaction", my_action);

Then, in the main program, be able to look up the function for "myaction" string, and if it exists, call it.

I reckon it has to do with mapping a string to a pointer to a function. But, honestly, I have no idea if it's possible or where to start.

Is such a thing possible in C? If it is, then, how can I do it?

1 Answers
Related