c - call a function by "string" -


I wonder if it is possible to do something like this:

  call ("myfunction" );   

And to call it, a long launch switch or statement is not implemented by calling a function called MyFunction.

There is another way to do that, which I actually implement the IRC protocol, which is to receive messages, and based on what I want to call the APPriet function

Please tell me how I am new to C and have completed the things!

There is no way to directly call a function such as string in the standard C library. If it was C ++, you can make std :: map to string to work on the pointer but not in C. You probably have to resort to a series strcmp s if C ++ is not an option.

  / * These are your handler functions / / zero user_fn () {printf ("user fn \ n"); } Zero pass_fn () {printf ("pass fn \ n"); } / * Stores a C-stonge with the function pointer * / typingform_fn_table_entry {char * name; Zero (* fn) (); } Fn_table_entry_t; / * These are tasks for calling each order * / fn_table_entry_t fn_table [] = {{"USER", user_fn}, {"PASS", pass_fn}}; / * Fn_lookup is a function that gives the pointer to the function for the given name. The function is not found or if the name is null, then it receives zero * / zero (* fn_lookup (const char * fn_name)) () {int i; If (! Fn_name) {return tap; } (I = 0; ii sizeof (fn_table) / sizeof (fn_table [0]); ++ i) {if (! Strcmp (fn_name, fn_table [i] .name)) {return fn_table [i] fn ; }} Return tap; } Int main () {fn_lookup ("USER") (); Fn_lookup ("pass") (); }    

Comments