3

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();
1
  • if you dont want to use if, use switch Commented Dec 20, 2016 at 1:18

1 Answer 1

7

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.