0

is it possible to access variable value through other variable, as below

char var[30];
char buffer[30];

strcpy(buffer, "ABC");

/*variable var is holding the "buffer" variable name as string*/ 
strcpy(var,"buffer")

is there a way to access the buffer variable value "ABC", through variable var. ?

Jesse Good
  • 50,901
  • 14
  • 124
  • 166

5 Answers5

3

Not in any practical way in C, and you don't really want to anyway. Tying your program logic to the names of your variables is a horrible idea. Typically I see people attempt this when what they really need is some sort of collection type (and array, a map, whatever).

How about filling us in on the problem you are trying to solve with this?

Per your comment:

I need to have dynamic debug messages, I have a file which contain each function variables that I want to print.

Use stringification in a macro:

#define str(s) #s

int main() {
    int bar;
    str(bar) /* replaced by "bar" */
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I need to have dynamic debug messages, I have a file which contain each function variables that I want to print. – Tarek Abul Naga Jun 20 '12 at 00:04
  • I'm confused, do you mean to access the symbol table? Scalars are named but they they contain values. In C the executable does not contain symbol names. For thay you have to look at symbol table which is output from the compiler. – starbolin Jun 20 '12 at 00:17
  • not symbol table my dear, it is normal text file, our devlopment team is thinking to put the variables that they wish to print thier values, into a text file, then print these variables value, we don't want to modify the program by putting printf each time when we need to know the variable value. – Tarek Abul Naga Jun 20 '12 at 00:52
  • @TarekAbulNaga: so you want to print the value of arbitrary variables from anywhere in the program? Well I can't help you there, sorry. – Ed S. Jun 20 '12 at 00:59
1

Not without significant boiler plate code. Variable names are eliminated at compile-time.

In theory, you could store a map from variable names to a pointer to a variable.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
1

No, you can't. If you want indirect access, then declare a pointer and assign var to it.

char *forVar = var;

// Now you can access/modify via [] operator.
Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

You can try using a union

For example:

union example {
    char var[40];
    char buffer[40];
} e1;

strcpy(e1.var, "ABC");

printf("%s is same as %s", e1.var, e1.buffer);
TheAJ
  • 10,485
  • 11
  • 38
  • 57
  • I need to have dynamic debug messages in my C program. I have a file which contains each function variables that I want to print. I am reading this file and stored the variables name in a variable called for example L_sVarName, so L_sVarName is having the name of the variable that I want to print it's value. That’s why I post my question L_sVarName =”L_sName” /* The variable in the file which I want to print */ L_sName =”ABC” So I need to find out a way to print the variable L_sName value “ABC” by L_sVarName – Tarek Abul Naga Jun 20 '12 at 00:18
0

So basically you want to log some variables when they are written to/read from/passed to functions etc?

Doing this with compiled C is difficult, as mentioned, especially if you are optimising your executable (-0n on the compile statement), in which case some of the variables can disappear completely, or get re-used by other variables.

Using GDB

Having said that, I know gdb can log variable access and that sort of stuff. If you were to write a python script (see http://sourceware.org/gdb/onlinedocs/gdb/Python.html), you should be able to write a script to log variable content.

See this question for more: Do specific action when certain breakpoint hits in gdb

On demand only, using pre-compiler scripting

Alternatively, if you just wanted to do it on demand, you'd be better off using a pre-processing script to add in custom macro's or similar, using a csv file as input:

for each line in CSV:
  read variable_name, file_name, variable_type
  find all occurrences of variable_name in file_name
  for each occurrence
    insert printf(....)

On demand only, using macros

Good luck. There isn't a nice way to do it, because you'd generally need some sort of lookup, and you'd need a way of selectively outputting variables to your print function. In some cases you could do something like:

static char _LOGGED_VAR_001 = 'z';
static char _LOGGED_VAR_002 = 'z';

#define cMyVar           _LOGGED_VAR_001
#define LOG_ALL_VARS()   printf("Vals: %c, %c", _LOGGED_VAR_001, _LOGGED_VAR_002)


void myFunc()
{
   char cMyVar; // Gets macro'd to _LOGGED_VAR_001 with LOCAL scope
   cMyVar = 'a'; // LOCAL scope _LOGGED_VAR_001 becomes 'a'
   LOG_ALL_VARS(); // At this point you print out the LOCAL _LOGGED_VAR_001 
                   // and the global _LOGGED_VAR_002
}

You would get Vals: a, z

This is pretty ugly, would only work for variables with local scope, messes round with memory consumption, could be error prone and is generally a bad idea.

On demand, heap/stack dump

If you have enough storage, you could dump all your memory on demand to a file. This probably wouldn't be very portable, depending on how it was done. I'm not sure how to do it though, in a reliable manner.

Recommendation

Use GDB for design-time diagnostics. It's what it's designed for :)

For (e.g.) analysing released code (automated bug reports), a full dump and then analysis at will might be relevant.

Community
  • 1
  • 1
chrisb
  • 393
  • 2
  • 10