Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable character inside a constant

Having defined this:

int var1 = 1;
int var2 = 2;
int var3 = 3;

I want to make this:

int result = varc * 70; // Where c is a previously defined int that can take 1,2 or 3 value.

Solutions? Thank you.

like image 412
user6626956 Avatar asked Sep 14 '26 07:09

user6626956


1 Answers

In C you're out of luck on this since it's not a reflective language. That is you can't get the value of a variable by somehow "stringifying" the name you gave it in the source code.

But what you could do is use an array:

int vars[] = {1, 2, 3};

int result = vars[i] * 70;

where i is 0, 1, or 2.

like image 159
Bathsheba Avatar answered Sep 16 '26 20:09

Bathsheba