0

I am stuck with a situation where I need to give array name from string variable.

Basically I want to create an array with same name as value in another string variable "name":

char *name="arr_name";

In my case the string being hold by variable name may change. Hence advice accroding.

Thanks!

5
  • 1
    Why would you do that?
    – Holt
    Jul 7, 2014 at 20:20
  • 3
    C is not an interpreted language
    – Ed Heal
    Jul 7, 2014 at 20:21
  • 3
    C can't do what you want, but at the same time there's probably no reason to want it. What do you wish to achieve?
    – MicroVirus
    Jul 7, 2014 at 20:21
  • It's might be possible through the preprocessor, though, I can't imagine why it would be necessary. Jul 7, 2014 at 20:35
  • There was a question like that 1-2 days ago. It had good answers. Umm... here stackoverflow.com/questions/24546385/…, it's very related. It's explained why it's quite impossible, why, and what can you do instead. It's for c++ but most of the point hold true. Only the specific work around solutions might need to be adapted, still worth of reading.
    – luk32
    Jul 7, 2014 at 20:46

1 Answer 1

0

I think you're looking for some mechanism, similar to the ones found in the higher level languages as in python (introspection), or C# (reflections). C doesn't provide this kind of insight from the runtime, not even the variable names are existing in the bytecode - so basically it's not possible and doesn't make any sense in the terms of the way how C works.

I don't know if that helps, but one thing you could do is to statically (so in the compilation time, not while it's running!) populate char* and create variable with the same name, given that the value of the string is a proper name for the variable (Naming convention for C/C++). You can achieve that by defining a proper macro (#define your_macro(...) code_to_populate_char_and_declare_variable), but I cannot see any point in doing so.

Not the answer you're looking for? Browse other questions tagged or ask your own question.