I would like to pass a string to a function in C in which the function will parse out a function name, the arguments for the function, and its datatypes, and then call the function for me. What is the best approach?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to write a function with a format string and variable arguments like:
like, say printf, sprintf, or scanf does,
then the best thing for you to do is look at some good tutorials.
http://msdn.microsoft.com/en-us/library/fxhdxye9(v=vs.80).aspx
http://www.cprogramming.com/tutorial/c/lesson17.html
If you are wanting to actually pass a function name for the function to call, along with its arguments, you either need to implement some form of reflection or introspection in your c code, a really complex switch statement which calls the functions for you based upon the string value, or write some complex macros to act as a sort of a secondary
compiler.
glib’s gobject is an excellent example of introspection in c.
http://developer.gnome.org/gobject/stable/
something simple without introspection may be:
If you have something more specific in mind, please specify in your question.