Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generate variable names

Reply
Thread Tools

generate variable names

 
 
panthera
Guest
Posts: n/a
 
      06-07-2007
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-07-2007
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.)

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Karl Malbrain
Guest
Posts: n/a
 
      06-07-2007
"panthera" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-07-2007
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
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-08-2007

"panthera" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) 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*/ )))


 
Reply With Quote
 
Johan Bengtsson
Guest
Posts: n/a
 
      06-09-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
Macro to iteratively generate variable names Marcus Kwok C++ 6 04-05-2006 03:46 PM
How to generate variable labels for same component within a generate loop Weng Tianxiang VHDL 5 02-16-2006 01:45 PM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
logical puzzle: how to generate reasonable archive file names fromfile and directory names fBechmann Python 0 06-10-2004 07:13 AM



Advertisments