Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Need to use a running variable name in c++

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

1 Solution
please check the following code.
for (i=1; i <= 3; i++)
       array1[i] = i;
       array2[i] = i;
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;

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now