Need to use a running variable name in c++

Posted on 2009-02-11
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

Question by:Mannsi
    LVL 6

    Expert Comment

    please check the following code.
    for (i=1; i <= 3; i++)
           array1[i] = i;
           array2[i] = i;
    LVL 45

    Accepted Solution

    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now