Is there any way we can form and execute a statement at runtime in C?
Is there any way we can form and execute a statement at runtime in C?
Well, yes and no. You can't form a C expression and run it, but you could generate code at runtime - there is also no standard way of doing that, it changes both with processor architecture and OS architecture how you go about this.
What are you actually trying to do? As in, do you have a particular problem you are trying to solve, and if so, can you explain what the problem is?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
There are three integer variables x1, x2 and x3 having values 1,2 and 3 respectively. Another fourth array variable x contains either x1, x2 or x3. I want to print the value of the variable which is the value of x. e.g. if x contains x2, I want to display 2. (Of course this is a simplification of a bigger program).
Sounds like all you need is a pointer to the right variable [somehow you must know which variable you want printed somewhere, right?]
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes.
So I take it that solved the problem?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Not really! I was thinking of this in a larger perspective. For this small scale problem, we can use if or swich statement or a program of the following kind. But I wanted to know whether there is any other technique through which we can form a statement and execute it at runtime.
Code:#define GET_VAL(x) (strcmp(x, "x1") == 0) ? x1 : ( \ (strcmp(x, "x2") == 0) ? x2 : ( \ (strcmp(x, "x3") == 0) ? x3 : 0)) main() { int x1 = 1; int x2 = 2; int x3 = 3; char x[20]; printf("Enter value of x "); scanf("%s", x); printf("Value is %d\n", GET_VAL(x)); }
Thanks
You could use an array of ints instead of 3 different ints, then just take an index number instead of a variable name from scanf(). Or if the user really feels the need to put x before the number, just strip off the x from the input, convert the rest to an int and use it as the array index.
That also will work. But my original point is different. I wanted to know whether there is any other technique through which we can form a statement and execute it at runtime.
Thanks
Soham
Sounds like what you want to do is dynamically create/reference variable names.
But I am pretty sure that is impossible.
edit: or maybe you can with macros which I have little experience with
So do you want the user to be able to enter something like this:
and have your program compile and run that code, or do you just want simple arithmetic statements?Code:sprintf( str, "a = %d", i );
If you want to write your own C shell, it'll probably be pretty damned hard & time consuming.
I want to execute it also. I think it is not that easy. I wanted something like Dynamic SQL from Oracle.
ANyways, thanks.
You could generate temp .c file and compile/execute it... or maybe there's some "library" for compiling code...
EDIT: or compile into shared library, and dlopen() it in current context...
So, are you writing something like a calculator/expression parser/spreadsheet application?
If so, you probably should consider implementing a "variable storage".
If you want a way to use/get variables out of your application for debug purposes or some such, you could add some code to store a name and variable, e.g.
--Code:#define ADD_VAR(t, x) add_var(#t, #x, &x,) #define REMOVE_VAR(x) remove_var(#x) void add_var(const char *type, const char *name, void *addr) { // Store type, name and addr in some sort of stack/list/array/etc. } void remove_var(const char *name) { // remove the last added variable of that name. } const char *get_fmt(const char *type) { const static struct { char *type; char *fmt; } table[] = { { "int", "%d" }, { "char *", "%s" }, { NULL, NULL } }; int i; for(i = 0; table[i].type; i++) { if(strcmp(type, table[i].type) == 0) return table[i].fmt; } return NULL; } void print_var(const char *name) { // Find name in list. if (found) { const char *fmt = getfmt(found->type); char buffer[100]; if (!fmt) printf("Unknown format %s\n", found->type); else { sprintf(buffer, "%s = %s\n", name, fmt); printf(buffer, *found->addr); } } else { printf("Could not find variable\n"); } } int main() { int x = 1; int y = 7; char *str = "foo"; char *name; ADD_VAR(int, x); ADD_VAR(int, y); ADD_VAR(char *, str); scanf("%s", name); print_var(name); REMOVE_VAR(str); REMOVE_VAR(y); REMOVE_VAR(x); }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.