On Thu, 2 Oct 2008 19:21:25 -0700 (PDT), sinbad
<si***********@gmail.comwrote in comp.lang.c:
how can i write a program, which should accept input as NAME of a
variable in the program and
print it's value.Is this possible. for example.
int main ()
{
int x,y,z;
x = 10;
y = 20;
z = 30;
wait_for_input();
print_output();
}
wait_for_input ()
{
do some thing to get input from user;
}
print_output()
{
do some thing to print output;
}
the user gives the following commands at runtime.
#print x --it should print value of x
#print y --it should print vlaue of y
The FAQ for this group does not cover this exact situation, but it
does cover a similar one with a very similar solution:
Q: If I have a char * variable pointing to the name of a function, how
can I call that function?
You can read the answer to this question here:
http://c-faq.com/misc/symtab.html
It should be quite easy to figure out how to modify this to print the
values of various objects of the same type.
The solution should work even for structure variables.
You can use this technique to do that as well, it just takes more work
on your part.
thanks
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html