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?
-
It doesn't make any sense to "make [a] string as a function call". – Cody Gray - on strike Jan 31 '12 at 05:15
-
2I'd love to help, but I can't make any sense out of your question. Please revise and try again. – Jonathan Henson Jan 31 '12 at 05:16
-
@CodyGray and Jonathan Henson i am working something on variable number of parameters.so in that for every different function call i'll get different list of parameters,so that is why i am forming a string with all the parameters. – johnkrishna Jan 31 '12 at 05:20
-
1Are you trying to replicate the behavior of `printf()`? – Cody Gray - on strike Jan 31 '12 at 05:22
-
@CodyGray no,this is a part of my JNI code – johnkrishna Jan 31 '12 at 05:23
-
JohnKrishna, that doesn't really answer his question. The language and use is irrelevant. What he means is, are you trying to write a function that takes a format string for the first parameter, and a variable number of arguments which will be specified in the format string? – Jonathan Henson Jan 31 '12 at 05:26
-
@JonathanHenson no i am not writing anything which makes use of format string. – johnkrishna Jan 31 '12 at 05:31
-
2The tool that "makes strings into function calls" is generally some kind of *compiler*. If you want to support general C syntax, then you need a general C compiler. If you want to support a more limited syntax, then you'll have to define *exactly* what that is, and write a small compiler for that. – Greg Hewgill Jan 31 '12 at 05:34
-
If you want to do such things on a regular basis, you'd better use a language with `eval` (such as JavaScript, for example). C is not quite suitable for this, unless you're ok with something like `CINT`. – SK-logic Feb 06 '12 at 08:18
4 Answers
If you want to write a function with a format string and variable arguments like:
int function(const char* strFormat, ... )
{
//parse out the format using regex or something
//then store the data into the variable aruments
//or create a string concatenating everything
}
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:
void* function (const char* strFunctionName, ... )
{
if(!strcmp(strFunctionName, "functionA"))
{
//use va_list to parse out the arguments for the function.
functionA(//each of the arguments from va_list);
}
else if(!strcmp(strFunctionName, "functionB"))
{
//use va_list to parse out the arguments for the function.
functionB(//args from va_list);
}
...
}
If you have something more specific in mind, please specify in your question.

- 8,076
- 3
- 28
- 52
-
thanks for your time,but you didn't get my question.i am not trying a function with variable number of arguments.what i am trying is suppose i have a string char *str="variance(dArray,p1,p2)".how to make this string as a normal function call – johnkrishna Jan 31 '12 at 05:44
-
@johnkrishna Using variable arguments will make the parsing of the datatypes and their values much easier than writing an incredibly complex regex to do it. – Jonathan Henson Jan 31 '12 at 05:56
-
thank you very much once again but we both are are on different tracks it seems.in my scenario every time string will keep change,depending on the parameters i receive,so i just need how to make my C compiler to treat my string as a function call. – johnkrishna Jan 31 '12 at 06:06
-
@johnkrishna Without writing your own compiler, you can't. One of my three options is your only option. You will have to write a function to execute the function specified in the string for you. for example: const char* strFunction = "MyFunction(a, b)"; ExecuteMyFunctionPlease(strFunction); There is no way that I know of to get around this. – Jonathan Henson Jan 31 '12 at 06:08
-
yes you are absolutely correct.but what if the value of strFunction changes? – johnkrishna Jan 31 '12 at 06:20
-
You will have to enforce a contract in your code that says that any function that expects to be callable dynamically i.e. from a string, has to make sure it is implemented in whatever function you write to handle this. I have already explained your three options for doing this, making sure it is implemented is up to you. – Jonathan Henson Jan 31 '12 at 06:22
-
@johnkrishna The implementation I put in my answer is probably the easiest. – Jonathan Henson Jan 31 '12 at 06:23
-
your answered helped me a lot,though this is not an exact answer for this question,marking it as accepted answer. – johnkrishna May 29 '12 at 09:38
I'm not sure if this is standard, but you could use __PRETTY_FUNCTION__
in C++.
e.g:
void foo(int a, int b)
{
cout << __PRETTY_FUNCTION__ << endl;
}
outputs:
void foo(int, int)

- 181,706
- 17
- 308
- 431

- 2,084
- 7
- 24
- 38
If you want to make:
char *funct = "foo(bar)";
to actually call the function foo()
, then its impossible, C just doesn't work that way. I would recommend that:
a) you sit down and rethink your application, because such behaviour shouldn't be needed for anything.
b) try a language with such capabilities, such as objective-c
c) if you really, really, really wanna do it... then make a string parser and lots of if's, or function pointers. Something in the lines of:
- Get string in the form of "function(var1, var2, ..., varN)"
- Get the name of the function (ie. everything before '(' )
- Get all the parameters (
strtok()
for example) - Identify the function comparing constant names with your string (
strcmp()
) - If the parameters are numeric (
isdigit()
orsscanf( string, "%d", ¶meter ) > 0
) convert them to a usable primitive (atoi()
,atof()
, etc orsscanf()
) - Pass the params to your function.

- 2,109
- 1
- 25
- 26
-
thank you.i think your option c will be helpful for me.is there a way in C++? – johnkrishna Jan 31 '12 at 10:20
-
@johnkrishna There is no way to do this in any language that doesn't support reflection. Your c-like options if you want to do this are Java, Objective-C or C#. Or you could implement the reflection yourself in c or in c++. – Jonathan Henson Jan 31 '12 at 15:42
-
@whitlionV here the parameters of the function will keep change,in that case how to identify the types(because strtok() will give all of them as strings) – johnkrishna Feb 06 '12 at 08:15
If you want to execute a code from a string (I'm just guessing, your question is hard to understand), then there are many options available: from embedding the whole Clang and LLVM into your code (and making sure its JIT is aware of all your declarations you're going to export) to embedding a simple scripting language (like Lua or Guile) and providing wrappers for all the functions you want to call this way.

- 9,605
- 1
- 23
- 35
-
ya i want to execute the method which is in string.but this is in C/C++.to understand my question properly see this http://stackoverflow.com/questions/9156389/how-to-parse-a-string-so-that-compiler-executes-the-statements-inside – johnkrishna Feb 06 '12 at 08:08
-
No, your question is still very far from being clear. And, btw., `Clang` is a C compiler. You may also want to try embedding `tcc` (it is made specifically for this purpose), it is much smaller and faster than clang. – SK-logic Feb 06 '12 at 08:13