C program to get variable name as input and print value

前端 未结 4 990
一整个雨季
一整个雨季 2021-01-16 16:58

I have a program test.c

int global_var=10;
printf(\"Done\");

i did

gcc -g test.c -o test

My query is I

4条回答
  •  轮回少年
    2021-01-16 17:51

    General answer: No. There is no connection between a variable name and its string representation (you can get the string representation of a variable name at compile time with the preprocessor, though).

    For identifiers with external linkage, there are (platform-dependent) ways: See e.g. dlsym for POSIX systems.

    You can compile with debugging information and access (most) variables by names from input. Unless you really write something like a debugger, this would be a horrible design, however (and even then, you don’t access the variables used in the debugger itself but of the programme being debugged).

    Finally, you could implement your own lookup table mapping from string representations to values.

提交回复
热议问题