We help IT Professionals succeed at work.

Need to use a running variable name in c++

Mannsi
Mannsi asked
on
Medium Priority
269 Views
Last Modified: 2012-05-06
Hello experts,

Maybe this is a trivial question but I haven't figured it out. I need to reference variable names based on a loop counter. A stupid example would be the "code" below.

I could do this with the Me() function in VB. How do I do this in c++ ?
int array1[10];
int array2[10];
 
for (i=1;i != 3;i++)
{
       array'i'[i] = i
}
       //Here I would like to put the number 1 in array1[1] and 2 in array2[2]

Open in new window

Comment
Watch Question

Commented:
please check the following code.
for (i=1; i <= 3; i++)
{
       array1[i] = i;
       array2[i] = i;
}
CERTIFIED EXPERT
Top Expert 2006
Commented:
you cant have running variable names afaik but you can use pointer arrays to have a similar effect

int arr0[5] = { 0 };
int arr1[5] = { 0 };
int arr2[5] = { 0 };
int arr3[5] = { 0 };
int arr4[5] = { 0 };

int * arr[5] = { arr0, arr1, arr2, arr3, arr4 };


for (i=1;i != 3;i++)
{
       (*(arr+i))[i] = i;
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.