Lets say I have the function
void do_work (foo?);
The ? represents a 0 or a 1 which will be the value of variable
int bar = 0;
There are also the variables
int foo0 = 0;
int foo1 = 0;
I am trying to inject the value of one variable into the name of another variable so that the function gets the full name of either variable depending on preceding logic.
How can I do this?
(In this situation, i’m not worried about good or bad practice.)
C doesn’t work in the way that perhaps you are thinking. You cannot dynamically refer to the name of a variable at runtime like you may be used to in other languages.
That said, the name of the function’s arguments is irrelevant.
You don’t have to match up the name of
argwithfoo0orfoo1. Now you can pass either variable into the function:Now the function
do_workwill be working on a copy of the variable passed. So if you change the variable’s value inside the function, it will still remain the same outside the function. You can change that by returning a new value:Lastly, it sounds like you want to use an array: