Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   newbie question on evaluating a string variable (http://www.velocityreviews.com/forums/t314793-newbie-question-on-evaluating-a-string-variable.html)

John Boik 08-16-2003 06:02 PM

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



Richard Heathfield 08-16-2003 06:12 PM

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

Malcolm 08-16-2003 07:03 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57