Reputation: 177
How can i use a variable(that I would get from user input) to call a function ?
char user_function[10];
scanf("%s", user_function);
user_function(); //Calls the function named user_function that the user typed
(without having something like this)
char user_function[10];
scanf("%s", user_function);
if( strcmp(user_function, 'printf()'))
printf();
Upvotes: 3
Views: 1863
Reputation: 29266
You can't.
At some point you have to look at the string contents and use those to call a particular function.
You can "hide it" a bit by doing something like a lookup table/hashmap of strings to function pointers, but in the end it is still just "look at the string and decide what to call"
Upvotes: 7