for loop on different variables names

后端 未结 4 822
野趣味
野趣味 2021-01-27 12:09

I am programing for a while but for this i couldn\'t find an answer yet .

lets say i want to run on a loop when i change the names of the variables.

lets say i h

4条回答
  •  萌比男神i
    2021-01-27 12:37

    If you absolutely have to keep these variables separate, then the only option you have is to "index" your variables by using an array of pointers and access your variables through it

    int ran1, ran2, ran3;
    
    int *rans[3] = { &ran1, &ran2, &ran3 };
    
    for (int k = 0; k < 3; k++)
      *rans[k] = k;
    

    (But maybe you should have used an int array instead of three separate variables from the very beginning.)

提交回复
热议问题