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
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.)
You can simple achieve this with defining an array as int ran[3]
and then writing below code to get it:-
ran[k % 3] = k;
Make it an array! Using different names differentiated by a number is a bad practice:
int ran[3];
for (int k = 0; k < 3; k++)
{
ran[k % 3] = k;
}
Now, instead of using ran1
, or ran2
, you would use ran[1]
or ran[2]
.
Arrays in C are quite confusing, and they are distinct from pointers.
uses arrays:
int ran[3];
for(int k=0;k<3;k++)
ran[k]=k;