Tarek Abul Naga
Tarek Abul Naga

Reputation: 1

Indirect variables access in C not C++

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. ?

Upvotes: 0

Views: 1561

Answers (5)

chrisb
chrisb

Reputation: 393

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.

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124770

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" */
}

Upvotes: 3

Mahesh
Mahesh

Reputation: 34645

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.

Upvotes: 1

tskuzzy
tskuzzy

Reputation: 36476

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.

Upvotes: 1

TheAJ
TheAJ

Reputation: 10875

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);

Upvotes: 0

Related Questions