program should print it's own variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sinbad

    #1

    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
  • Jack Klein

    #2
    Re: program should print it's own variables

    On Thu, 2 Oct 2008 19:21:25 -0700 (PDT), sinbad
    <sinbad.sinbad@ 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:



    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.l earn.c-c++

    Comment

    • sinbad

      #3
      Re: program should print it's own variables

      On Oct 3, 8:27 am, Jack Klein <jackkl...@spam cop.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:
      >

      >
      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.l earn.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

      Comment

      • Nate Eldredge

        #4
        Re: program should print it's own variables

        sinbad <sinbad.sinbad@ 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.)

        Comment

        • Malcolm McLean

          #5
          Re: program should print it's own variables


          "sinbad" <sinbad.sinbad@ 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.


          Comment

          Working...