Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Declaring multiple int with for loops in C

I am trying to declare multiple int with the help of for loops in c.

The code almost looks like

for (int x = 0; x < 45; x++){
    int valx = 10*x;
}

The goal is to have a variable called val0 = 0, val1 = 10, val2 = 20……
I also need to be able to reference these made int later in the code and modify them, etc.
Is there a coinvent to do this? If not is there another effective way to declare 45 variables fast? Also, I know the code is wrong I am trying to see if there is something similar. I think someone mentioned to use struct. How would that work in regard to this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want to create an array outside of the loop and assign to that.

int val[45];
for (int x = 0; x < 45; x++){
    val[x] = 10*x;
}

It wouldn’t make sense to create the variables inside of the loop because those variables would disappear after the loop finishes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading