Variable Variables in C++

cyberphant0m

Member
Oct 21, 2003
99
0
0
Im trying to have a variable name, depending on the value of a counter. Something like this:

int i;
for (i=1; i<=10; i++)
{
int vari = i;
}

This way, at the end of the loop, i would have the following variables:

var1 = 1
var2 = 2
var3 = 3
...
var10 = 10

How would I do this???
 

Smilin

Diamond Member
Mar 4, 2002
7,357
0
0
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: cyberphant0m
Im trying to have a variable name, depending on the value of a counter. Something like this:

int i;
for (i=1; i<=10; i++)
{
int vari = i;
}

This way, at the end of the loop, i would have the following variables:

var1 = 1
var2 = 2
var3 = 3
...
var10 = 10

How would I do this???


C++ doesn't work that way :) If you can tell us why you're trying to do this, then perhaps we can tell you how to accomplish it using the C++ way.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Smilin
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.

You only have to worry about zero-based indeces when you access array items (i.e. the first item is var[0], second item is var[1], last item is var[9]), the number you put in the declaration is the number of items. So:

int i, var[10];

for(i=0; i<10; i++)
var[ i ] = i+1;
 

PowerMacG5

Diamond Member
Apr 14, 2002
7,701
0
0
Originally posted by: BingBongWongFooey
Originally posted by: Smilin
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.

You only have to worry about zero-based indeces when you access array items (i.e. the first item is var[0], second item is var[1], last item is var[9]), the number you put in the declaration is the number of items. So:

int i, var[10];

for(i=0; i<10; i++)
var[ i ] = i+1;

I think he knew that, that's why he declared it of length 11. He doesn't like standards :p. My your method (the more accepted, and IMO the better), the array would be declared of length 10 (0-9 indexes) not 11 (0-10 indexes).
 

cyberphant0m

Member
Oct 21, 2003
99
0
0
The reason I'm asking for variable variables is because I want to make a never-ending loop which creates a different variable each time through (can't write past the end of an array...) Something like this:

int i;
for(i = 0; i <= 10; i++)
{
int variablei = i;
}

What I'm looking to do is make a program that creates variables on the freestore and create a memory leak in a few seconds... (just to see what it would be like...)
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
if all you want to do is make a memory leak, then

for( ; ; ) //put in your own limits
malloc(10000); //change number of bytes to allocate to more or less, if you like
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
this will make a nice one.

void leako()
{
std::vector<int*> vari;
for (int i=1; i<=10; i++)
{
vari.push_back(new int);
}
}
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
You cannot create variables on the fly in C/C++.

Well not in the sense you want to.
Sciencewhiz gave the easiest way to create a memory leak. And probably the most common, create memory and don't delete it.
Repeat, It doesn't get any simpiler than that. You don't really need the variables, you need the memory. Thats what malloc or new is for.