program should print it's own variables

S

sinbad

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 solution should work even for structure variables.

thanks
 
S

sinbad

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.


You can use this technique to do that as well, it just takes more work
on your part.


--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp://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

The FAQ says that the program should maintain it's own symbol table
mapping all the variable-names to actual variables. If the program is
very
big having thousands of variables this doesn't scale well.

Is there any simple way of doing this. Though not simple, GDB has this
kind of
mechanism right? If my program has been compiled with GDB symbols in
it, can this
be done?

thanks
 
N

Nate Eldredge

sinbad said:
The FAQ says that the program should maintain it's own symbol table
mapping all the variable-names to actual variables. If the program is
very
big having thousands of variables this doesn't scale well.

Is there any simple way of doing this. Though not simple, GDB has this
kind of
mechanism right? If my program has been compiled with GDB symbols in
it, can this
be done?

That's also in the FAQ, where it talks about the nlist function. Of
course this is not portable beyond some Unix flavors, and will not
work if your executable has been stripped. I don't think there is a
portable, reliable way to do this without listing all the variable
names in your source. (Of course, this could be automated with a
script of some kind.)
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top