473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

generate variable names

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
5 2613
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
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
7
by: ben | last post by:
hello, an algorithm book i'm reading talks about the connectivity problem/algorithm. it gives a number of examples where the connectivity problem applies to real life situations (like, the...
6
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an...
22
by: Roberto Gori | last post by:
Hi all, i'm looking for a way, a tool or a macro like this: USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN) to generate the following code: void foo(type1 arg1, type2...
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
5
by: mortb | last post by:
Hello! I make use of anonymous delegates in my code. For debugging purposes I'd like to generate the code that the delegates describe. Do anyone have an easy way to do this? cheers, mortb
2
by: Tom | last post by:
I have a textbox on my web form were users can enter in 1 name or many names. The form then validates the name against the database. If the name is valid its then saved into a varaible to be used at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.