Reputation: 93
I am wondering if there is a way to define a global variable that has the name of a specific instance from my main function? I have to access an array by bit values so to do that I need to type:
state.reg[ 4 * mc_binary[ 18 ] + 2 * mc_binary[ 17 ] + 1 * mc_binary[ 16 ] ]
Can I have:
#define state.reg[ 4 * mc_binary[ 18 ] + 2 * mc_binary[ 17 ] + 1 * mc_binary[ 16 ] ] registerA
where state
is my instance of a stateType
struct that is used in my main function?
Upvotes: 1
Views: 111
Reputation: 76765
This is what I would recommend:
#define BIT_INDEX3(bit2, bit1, bit0) \
(4 * (bit2) + 2 * (bit1) + (bit0))
// use in code:
state.reg[ BIT_INDEX3(mc_binary[18], mc_binary[17], mc_binary[16]) ]
You don't want to get too tricky, but if you will often be pulling from the same array (as you did here) you could make another define for that:
#define BIT_INDEX_ARRAY3(a, i2, i1, i0) \
BIT_INDEX3((a)[i2], (a)[i1], (a)[i0])
// use in code:
state.reg[ BIT_INDEX_ARRAY3(mc_binary, 18, 17, 16) ]
Upvotes: 2