![]() |
newbie question on evaluating a string variable
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[i].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@ompress.com |
Re: newbie question on evaluating a string variable
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[i].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 : binary@eton.powernet.co.uk "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 |
Re: newbie question on evaluating a string variable
"John Boik" <john.boik@ompress.com> 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[i].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. |
| All times are GMT. The time now is 10:49 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.