1

For example: like I have a variable char str[3]="abc"; then can I use this "abc" as the name of another variable (say an integer) so that I can store any value in abc. If yes, then while storing a value in 'abc' like this: int abc=123; can I refer abc as variable by referring it through str?

MK Singh
  • 706
  • 1
  • 13
  • 36
  • No its not possible. Also, `char str[3]="abc";` is dangerous. "Strings" like this are terminated with a null (`\0`) character. This means you need the array to be at least 4 wide. – ArjunShankar Sep 11 '12 at 08:32
  • 1
    possible duplicate of [Reflection Support in C](http://stackoverflow.com/questions/1353022/reflection-support-in-c) – Fred Foo Sep 11 '12 at 08:34
  • 2
    MK Singh, I think what @Fredrik is getting at is that you may want to go back over your old questions and accept the answers (big honkin' green tick mark) that best answered the question. It's _possible_ that only one third of your questions have been answered satisfactorily but you should at least check :-) – paxdiablo Sep 11 '12 at 08:44
  • 1
    Look up the keyword: associative array. If you implement one in C, you'll be able to do things similar to what you want as paxdiablo shows in his answer. – Alexey Frunze Sep 11 '12 at 08:47

4 Answers4

5

No. What you're talking about is reflection, a way to access the inner details of the environment. C currently does not have this built into the language.

There are ways to achieve the same effect, such as having a mapping data structure from strings to integer pointers, but it's a bit messy. As one example:

int abc, def;
char *strName[] = {"abc", "def"};
int *address[] = { &abc, &def};
:
char *key = "def";
int newVal = 42;

for (i = 0; i < sizeof (strName) / sizeof (*strName); i++)
    if (strcmp (key, strName[i]) == 0)
        *(address[i]) = newVal;

This would go through the list of keys until it found a matching one, then use the equivalent pointer in the list of addresses to modify the variable.

But, in all honesty, once you're having to do something dark and devious like that, you may as well do it with a more appropriate data structure.

For example, a map where the values are stored in the actual map rather than it holding the addresses of "external" integers.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 4
    @MKSingh it's also possible in PHP. So what? – Luchian Grigore Sep 11 '12 at 08:32
  • Actually I have a string which stores the names of columns in a database. I want to create a function like char *get(char[] param); Now this param will be reading the string for different column names in the table. Whichever name comes in the param, my program should fetch the value in that column from the table. All columns are character type in the table. How can this be achieved – MK Singh Sep 11 '12 at 08:38
  • @MKSingh, that's a different question. It's quite easy to construct a select statement from the string and then dynamically execute it. – paxdiablo Sep 11 '12 at 08:39
  • Please note that not even reflection deals with managing variable names into runtime. Variable names do *not* survive compilation, except for debug purposes. Java for instance has reflection, but you cannot use it to retrieve variable names – Jack Sep 11 '12 at 14:55
2

Not only you cannot, but it is a part of ideology of what C language is. Variable names do not exist at run-time, and variable contents should not matter at compile time.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
0

There is a difference of what you see of your program by looking at the source code, and what your "program itself sees" when running the raw binary executable. In the executable, there is no such thing as variable names.

Lets say we have this program:

char str[4]="abc"; // note that you need to allocate 4 bytes
int abc=123;

The above is the source code, that's what the programmer sees. The program itself only "sees" something similar to this:

At some address 0x12345678, there are 4 bytes. They are 0x61, 0x62, 0x63, 0x00 (the ASCII codes). The program will access theses as individual bytes, because of the char type in the source code. But the program actually doesn't see the type either.

Then at address 0x1234567C, 4 bytes further down in memory, we have another 4 bytes (assuming 32 bit CPU). They contain the value 0x0000007B (123). The program only refers to this int variable as 0x1234567C, it has no idea that the programmer calls it abc in the source code.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

You can consider code generation.

With an XML:

<var type="int" name="abc" />

Generate the code:

int abc;

(The input XML itself can itself be created with your char str[] as input)

gammay
  • 5,957
  • 7
  • 32
  • 51