generate variable names

P

panthera

Hi,

I have a problem regarding generating variable names in my
program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
array.And I want to randomly select one of them and get its value.So I
pick a random number between 0 and 9 and concat it with 'b' to get the
name of this variable.But how do I get the value of this variable?

I tried to define a macro using b##num,but it results in bnum instead
of b5 for example.

Thx

panthera
 
E

Eric Sosman

panthera wrote On 06/07/07 16:21,:
Hi,

I have a problem regarding generating variable names in my
program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
array.And I want to randomly select one of them and get its value.So I
pick a random number between 0 and 9 and concat it with 'b' to get the
name of this variable.But how do I get the value of this variable?

Use an array.

Yes, I know you said they are "NOT in an array."
That's a mistake; fix it.
I tried to define a macro using b##num,but it results in bnum instead
of b5 for example.

Wrong tool for the job. (Also, a tool that you do
not seem to understand yet.)
 
K

Karl Malbrain

panthera said:
Hi,

I have a problem regarding generating variable names in my
program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
array.And I want to randomly select one of them and get its value.So I
pick a random number between 0 and 9 and concat it with 'b' to get the
name of this variable.But how do I get the value of this variable?

Why don't you put the addresses of the variables into an array:

int *addr[10];

addr[0] = &b0;
....(etc)...

Then you can reference their values as *addr;

karl m
 
D

Default User

panthera said:
Hi,

I have a problem regarding generating variable names in my
program.

Variable names don't work that way, they aren't a run-time construct.
They are used by the compiler. You'll have to find a way to map
generated names at runtime to values or pointers stored in some manner.

One way is to have two parallel arrays, one with int values (or
pointers if you need to change the contents of the variables) and
another with names. You then construct your random name, find a match
in the name array, and access the variable value that has the same
index.




Brian
 
A

Army1987

panthera said:
Hi,

I have a problem regarding generating variable names in my
program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
array.And I want to randomly select one of them and get its value.
Why?

So I
pick a random number between 0 and 9 and concat it with 'b' to get the
name of this variable.But how do I get the value of this variable?

I tried to define a macro using b##num,but it results in bnum instead
of b5 for example.


*(num == 0 ? &b0 : (num == 1 ? &b1 : (num == 2 ? &b2 : /*etc*/ )))
 
J

Johan Bengtsson

Eric said:
panthera wrote On 06/07/07 16:21,:

Use an array.

Yes, I know you said they are "NOT in an array."
That's a mistake; fix it.
if you (for some to me very unlikely reason) can not use an array this
is a possible solution:

variable_type getValue(int number)
{
switch (number)
{
case 0: return b0;
case 1: return b1;
/* and so on*/
case 9: return b9;
}
return 0; /* or whatever you would like as a fail-safe value*/
}

substitute variable_type to whatever type your variables have.

But, as stated above: using an array is definitely preferable to this,
it is just another possible solution!
Also note that ten cases is somewhat reasonable, expanding the problem
to 10000 cases is probably not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top