4

Is the "destringification" possible in C89 just like stringification is possible?

Alan Salios
  • 241
  • 2
  • 9
  • 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 Answers2

2

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.

Michele De Pascalis
  • 932
  • 2
  • 9
  • 26
0

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.

user1969104
  • 2,340
  • 14
  • 15