Is the "destringification" possible in C89 just like stringification is possible?
-
Are you asking in general (in which case the answer is probably no) or are you trying to achieve a particular task by destringifying? – M Oehm Feb 03 '15 at 06:40
-
I am trying to access the variable, named in a string. A variable name can be compared with a string and its value can be accessed through a pointer in this case. The question is how to connect these things. – Alan Salios Feb 03 '15 at 06:50
-
Something like declaring `void* **list` and a macro `#define declare(type, name, init) type name = init; {//3dallocation, *list[num] = #name &list[num] = name;} ...` – Alan Salios Feb 03 '15 at 06:54
-
So it's really more like a symbol lookup. Why do you want this to be done via a string? Is the string provided by the user? – M Oehm Feb 03 '15 at 07:00
-
Yes. As a user input. The user input the name of the variable in the console and the console output its value. How isn't that useful.. especially for development libraries. – Alan Salios Feb 03 '15 at 07:01
-
It looks like your use case is for debugging? If so, gdb already handles this since you can print variable values at run time under gdb. – merlin2011 Feb 03 '15 at 08:21
2 Answers
Variables names are translated into symbol names at compile time. These symbols are supposed not to be accessible from inside the program and their names often get mangled, but anyway, C does not provide a way to access his own runtime from the inside. This kind of feature in a programming language is called "reflection", and if you rely on its existence for your project you should look up a language featuring it, or, as I would suggest, use some sort of 2D map or dictionary, which are data structures in which each object is associated with a unique key, often a string, making it easy to look it up in the way you seem to be needing.

- 932
- 2
- 9
- 26
I think you mean a macro DESTRINGIFY("hello") to be expanded to hello. In that case, NO, it is NOT possible in C89 as well as in later standards.

- 2,340
- 14
- 15
-
Well name identifiers must be stored as a string somewhere in the memory. I think DESTRINGIFY is possible on way way or another.. – Alan Salios Feb 03 '15 at 06:55
-
1@AlanSalios: No, they usually are not stored in memory, only identifiers with external linkage are. – mafso Feb 03 '15 at 07:07
-