newbie question on evaluating a string variable

Discussion in 'C Programming' started by John Boik, Aug 16, 2003.

  1. John Boik

    John Boik Guest

    Hello. I have what I think is a simple newbie question, but I have had no
    luck in finding an answer on the web or in books. I have a structure that
    contains a string. Call it Struct.string. I also have a (gsl) function
    that requires the string value that is contained in the structure. How do I
    convert the variable Struct[1].string to its literal value? For example, if
    "Bob" is the value stored in Struct[1].string, how do I convert the variable
    name Struct[1].string to "Bob"? C does not seem to have an Evaluate
    function to change a variable name to the value it contains.

    Thanks,
    John
    John Boik, Aug 16, 2003
    #1
    1. Advertising

  2. John Boik wrote:

    > Hello. I have what I think is a simple newbie question, but I have had no
    > luck in finding an answer on the web or in books. I have a structure that
    > contains a string. Call it Struct.string. I also have a (gsl)
    > function
    > that requires the string value that is contained in the structure. How do
    > I
    > convert the variable Struct[1].string to its literal value? For example,
    > if "Bob" is the value stored in Struct[1].string, how do I convert the
    > variable
    > name Struct[1].string to "Bob"? C does not seem to have an Evaluate
    > function to change a variable name to the value it contains.


    In a typical C implementation, identifiers are discarded on compilation. The
    string doesn't get a value until runtime. So you're basically asking for
    the impossible.

    Some high-level languages retain their symbol tables at runtime; in some
    such languages, what you ask might be possible.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Aug 16, 2003
    #2
    1. Advertising

  3. John Boik

    Malcolm Guest

    "John Boik" <> wrote in message
    >
    > Hello. I have what I think is a simple newbie question, but I have had >

    no luck in finding an answer on the web or in books. I have a structure
    > that contains a string. Call it Struct.string. I also have a (gsl)
    > function that requires the string value that is contained in the

    structure.
    > How do I convert the variable Struct[1].string to its literal value? For
    > example, if "Bob" is the value stored in Struct[1].string, how do I
    > convert the variable name Struct[1].string to "Bob"? C does not seem
    > to have an Evaluate function to change a variable name to the value it
    > contains.
    >

    As Richard pointed out, C variable names are stripped at compile time.

    If your program requires you to change the name of a variable at run time,
    you are probably not coding in a very C-like fashion. There is probably a
    better way of achieving whatever it is you are trying to do.

    However you might be confused by strings and identiifers.

    #include <stdio.h>
    #include <string.h>

    typedef struct
    {
    char string[64];
    } MYSTRUCT;

    void foo(char *str);

    int main(void)
    {
    MYSTRUCT astruct;

    strcpy(astruct.string, "Bob");
    foo(astruct.string);

    return 0;
    }

    void foo(char *str)
    {
    printf("Foo was passed %s\n", str);
    }

    This is a skeleton program showing how you would typically manipulate
    strings in C.
    Malcolm, Aug 16, 2003
    #3
    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. Shelly
    Replies:
    8
    Views:
    559
    Michael Borgwardt
    Dec 10, 2004
  2. Jason
    Replies:
    25
    Views:
    966
    Jorge Rivera
    Feb 22, 2004
  3. bugnthecode

    Re-evaluating a string?

    bugnthecode, Jul 23, 2006, in forum: Python
    Replies:
    6
    Views:
    443
    John Machin
    Jul 24, 2006
  4. heinous (sent by Nabble.com)

    Evaluating the string in a variable to use as a method name

    heinous (sent by Nabble.com), Mar 16, 2006, in forum: Ruby
    Replies:
    3
    Views:
    146
    Robert Klemme
    Mar 17, 2006
  5. C. N. Desrosiers
    Replies:
    13
    Views:
    166
    Fábio Santos
    May 25, 2013
Loading...

Share This Page