By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

generate variable names

P: n/a
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

Jun 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.)

--
Er*********@sun.com
Jun 7 '07 #2

P: n/a
"panthera" <pa**********@gmail.comwrote in message
news:11**********************@n4g2000hsb.googlegro ups.com...
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[i];

karl m
Jun 7 '07 #3

P: n/a
panthera wrote:
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
Jun 7 '07 #4

P: n/a

"panthera" <pa**********@gmail.comha scritto nel messaggio
news:11**********************@n4g2000hsb.googlegro ups.com...
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*/ )))
Jun 8 '07 #5

P: n/a
Eric Sosman wrote:
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.
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.
Jun 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.