How to convert from a string to an expression?

D

David

for example:
int x=10;
char *str="\"x=%d\",x";

if I use
printf(expression(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?
 
M

Michael Mair

David said:
for example:
int x=10;
char *str="\"x=%d\",x";

if I use
printf(expression(str));
then the results is
x=10

even I know it's not easy to return a expression from a function.

can u give me a good idea?

If you tell us what you want to achieve and give us the code
you have up to now or at least your representation of an "expression"
(which is no builtin C datatype).

Cheers
Michael
 
K

Keith Thompson

Michael Mair said:
If you tell us what you want to achieve and give us the code
you have up to now or at least your representation of an "expression"
(which is no builtin C datatype).

I think he's done that. His representation of an expression seems to
be a string containing a sequence of arguments to printf, separated by
commas. He wants a function

char *expression (char *expr);

that will return the string that would have been printed by printf().

David, is that about right?

Unfortunately, what you're trying to do is pretty much impossible.
Given a string "x", there's no way in C to get the value of the
variable whose name happens to be x. Variable names appear in source
code, but typically do not appear in the executable program; even if
they do, there's no way to get at them. Roughly speaking, the
compiler decides where x is going to be stored; all references to x in
your program are transformed into references to that memory location,
and the name vanishes.

Now if you explicitly associate the string "x" with the variable,
something like
record_name("x", &x);
you might be able to do something like what what you describe, but
it's going to be complicated. The above would not be adequate; you'll
also need some way to associate "x" with the type of x, which happens
to be int -- and there's no simple way to represent a type at runtime.

The first thing you should do is decide whether whatever problem
you're trying to solve can be solved in some other way.
 
M

Michael Mair

Keith said:
I think he's done that. His representation of an expression seems to
be a string containing a sequence of arguments to printf, separated by
commas. He wants a function

char *expression (char *expr);

that will return the string that would have been printed by printf().

Ah, I completely misread that. He sort of stringizes the arguments
he would normally pass to printf() and wants convert() to do
printf()s job but only with the string as input.

Hmmm...
[snip: Explanation of problems]
Now if you explicitly associate the string "x" with the variable,
something like
record_name("x", &x);
you might be able to do something like what what you describe, but
it's going to be complicated. The above would not be adequate; you'll
also need some way to associate "x" with the type of x, which happens
to be int -- and there's no simple way to represent a type at runtime.

Yep. Specialised solution:
One decides that a to z are "enough" and that all variables are
of either the largest integer or the largest floating point type.
Then you can store the addresses in an array:
void *vars['z'+1];

.....
unsigned long var[26], *ptmp, sink;
int i;
for (i=0, ptmp = var; i<='z'; i++)
if (isalpha(i) && islower(i))
vars = ptmp++;
else
vars = &sink;
(untested)
Alternatively, one could use NULL instead of &sink.
However, this is no general and no easily extendible _and_
maintainable solution.

Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top