1

Lets say I have the function

 void do_work (foo?);

The ? represents a 0 or a 1 which will be the value of variable

 int bar = 0;

There are also the variables

 int foo0 = 0;
 int foo1 = 0;

I am trying to inject the value of one variable into the name of another variable so that the function gets the full name of either variable depending on preceding logic.

How can I do this?

(In this situation, i'm not worried about good or bad practice.)

3
  • 1
    Are you aware that the variable names don't exist at the runtime? Commented Oct 14, 2010 at 5:51
  • Haha, you might think that would be obvious Commented Oct 14, 2010 at 5:54
  • 1
    @ruslik: That depends entirely on linkage. Global variables in shared libraries can be looked up at runtime (by name). Commented Oct 14, 2010 at 5:54

8 Answers 8

4

C doesn't work in the way that perhaps you are thinking. You cannot dynamically refer to the name of a variable at runtime like you may be used to in other languages.

That said, the name of the function's arguments is irrelevant.

void do_work(int arg)
{
} 

You don't have to match up the name of arg with foo0 or foo1. Now you can pass either variable into the function:

int foo0, foo1;

if (some_condition)
  do_work(foo0);
else
  do_work(foo1);

Now the function do_work will be working on a copy of the variable passed. So if you change the variable's value inside the function, it will still remain the same outside the function. You can change that by returning a new value:

int do_work(int arg)
{
  return arg + 1;
}

foo0 = do_work(foo0);

Lastly, it sounds like you want to use an array:

int foo[2]; // create two ints: foo[0] and foo[1]

do_work(foo[0]);

int i = 1;
do_work(foo[i]);
Sign up to request clarification or add additional context in comments.

Comments

4

By special request of @dreamlax:

1) declare these variables as

__declspec( dllexport ) static int foo1;
__declspec( dllexport ) static int foo2;

2) Use HANDLE this_module = GetModuleHandle(NULL); to obtain the current module

3) Obtain the name of the needed variable:

char vname[100];
sprintf(vname, "foo%d", bar);

4) Find it:

int* fooX = (int*)GetProcAddress(this_module, vname);

5) And, finally, you can use it:

  do_work(fooX);  // or *fooX, as you like

5 Comments

Platform dependent solution. And you are working with pointers, can't you use some simple hash?
Well, it was just a proof of concept.
-1 for teaching a newbie how to do something idiotic he wants to do.
+1. This is a wonderfully wrong thing to do: Take a static bound language and use its platform's ability to do late binding to symbols to compute a symbol name at run time. Although we all agree this is the wrong thing to do in this instance, it is the right answer to some problems. For example, the Lua interpreter does this when loading a module written in C from a DLL or .so file. The module's entry point has a name like luaopen_foo() where foo is the name of the module. That name is known only at run time when the require "foo" function call is executed.
This is never the right thing to do. It's always platform-specific and has no adaptation to arbitrary platforms without inventing your own dynamic linking system for the platform (which, in the worst case, involves writing a C compiler/linker to output bytecode and a bytecode interpreter). Even if it weren't for these issues, an answer to another potentially-interesting advanced usage question does not belong in the answers to a newbie's question.
3

Use an array or pointer instead. You would access it with, e.g. foo[ind].

Comments

2

Use array:

int foo[2] = {0, 0};

and then you can type

do_work(foo[bar]);

Comments

1

To keep the original foo1 and foo2 variables, you can set up an array with pointers to those locations, and read/write to the vairables via the pointers in the array.

I.e.

int *fooArray[2];
fooArray[0] = &foo0;
fooArray[1] = &foo1;

...then you can call your function with:

do_work(*fooArray[bar]);

Comments

0

arrays are designed to hold values of homogeneous data types and nobody will use foo0, foo1 for this kind of requirement

Comments

0

Since you say you're not concerned with good or bad practice, you could implement the behaviour you want without introducing any new variables:

if (bar == 0)
    do_work(foo0);
else if (bar == 1)
    do_work(foo1);
else
    puts("Uh oh, unexpected bar value!");

Comments

0

Here's a bad idea for you:

#define foo0 foo[0]
#define foo1 foo[1]
#define foobar foo[bar]

int foo[2];
int bar;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.