473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

program should print it's own variables

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
Oct 3 '08 #1
4 2142
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
Oct 3 '08 #2
On Oct 3, 8:27*am, Jack Klein <jackkl...@spamcop.netwrote:
On Thu, 2 Oct 2008 19:21:25 -0700 (PDT), sinbad
<sinbad.sin...@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.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
Oct 3 '08 #3
sinbad <si***********@gmail.comwrites:
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.)
Oct 3 '08 #4

"sinbad" <si***********@gmail.comwrote
>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?
The short answer is that C doesn't allow you to do what you want. In this it
is unlike some other languages such as Lisp, which keep symbols at runtime.

However often the symbols haven't been stripped from the executable. So if
you know the structure of the executable it might be possible to work to the
variable in memory. However there are all sorts of issues with optimisations
and dynamic memory that make this non-trivial. It is also
compiler-dependent. It's not something you can knock up as a quick debugging
aid.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
5
by: Henry Jordon | last post by:
hello I was wondering if someone could help me get a main going on this project I've completed the header file that the professor started us on but not really sure how to get the main going. If...
6
by: CJ | last post by:
Hey, I'm new to the Python world, but I do have experience in several other languages. I've been running through a tutorial, and I decided that I'd make a program that runs through a list, finds if...
5
by: cj | last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first program. I have an example of one that I need to write about. Any help getting me started is appreciated. I'm having trouble...
3
by: vatsa | last post by:
Hi all, we can simply change value of PS1 from command prompt . but I want to change the value of PS1 from within a c program. i tried to do that using getenv & putenv. but it is n't...
1
by: coolindienc | last post by:
I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where...
1
by: coolindienc | last post by:
I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where...
2
by: toprahman | last post by:
Hi guys, I'm a student teacher at a high school in Washington D.C. and I've come across a slight problem. See, I'm a math major, but one of the computer science teachers just quit and they need me to...
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.