newbie question on evaluating a string variable

J

John Boik

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
(e-mail address removed)
 
R

Richard Heathfield

John said:
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.
 
M

Malcolm

John Boik said:
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.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top