[LCP]Printing a Variables Value..!!!

Steve Baker ice at mama.indstate.edu
Fri Jun 20 23:36:01 UTC 2003


"J.Mohamed Zahoor" <zahoor at midascomm.com> wrote:
> main()
> {
>    int aaa =10, bbb =20, ccc =30;
>    char varName[20];
>    
>    memset(varName,0,20);
>    printf ("Enter any variable name\n");
>    gets(varName);  /* Assume i entered aaa */
>
>    Do something here to print the value of aaa
>    without implementing additional data structures 
>    like constructing a jump table that maps variable
>    name string with that variables address...
> }
>
> This can be done by searching the symbol table with the
> variables name... But that is too cumbersome.. is there
> a simple way to do this...!!!!

  Perhaps you could make a shared library with all the variables you wish to
inspect, open that library with dlopen() and then you can use dlsym() to look
them up. I suppose it may be possible to have your entire program be the
shared library save for the start program which would just dlopen the library
then transfer control to the library passing to it the library handle from
dlopen().  That way you should be able to access variables normally and still
be able to use dlsym() to reference them by name.  Not too hackish.

  A hashed jump table or the like would still probably be the least hackish
solution, assuming you have a known set of variables to inspect.

								- Steve



More information about the linuxCprogramming mailing list