Reputation: 35
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.
Upvotes: 3
Views: 65
Reputation: 1045
you write:
int result = varc * 70;
This is what you want to make is not possible in language c.
Note: varc is an identifier
Remember IDENTIFIER in C : Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions.
It must be unique for all entities and also an identifier is a string of alphanumeric characters
Ok, you remembered. :)
So, you never used "c" present in "varc" to treat(refer) to other variables/identifies/entities.
I hope I might be solve your doubt in easiest way .Thank you! :)
Upvotes: 2
Reputation: 234685
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.
Upvotes: 6