How to convert from a string to an expression?

Discussion in 'C Programming' started by David, May 19, 2005.

  1. David

    David Guest

    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?
    David, May 19, 2005
    #1
    1. Advertising

  2. David

    Michael Mair Guest

    David wrote:
    > 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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 19, 2005
    #2
    1. Advertising

  3. Michael Mair <> writes:
    > David wrote:
    >> 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).


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 19, 2005
    #3
  4. David

    Michael Mair Guest

    Keith Thompson wrote:
    > Michael Mair <> writes:
    >
    >>David wrote:
    >>
    >>>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).

    >
    > 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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 19, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Banks

    Convert string to a string array

    Andrew Banks, Apr 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    57,960
    Andrew Banks
    Apr 19, 2004
  2. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Connection String object Convert to String Variable Type

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,047
    Norman Yuan
    Oct 26, 2004
  3. spiros
    Replies:
    7
    Views:
    482
    John Harrison
    Jul 20, 2004
  4. rajesh
    Replies:
    0
    Views:
    168
    rajesh
    Nov 23, 2005
  5. Replies:
    2
    Views:
    169
    John Bokma
    Aug 30, 2013
Loading...

Share This Page