Is is possible to define a macro BREF(…):
struct bits
{
int b0:1;
int b1:1;
int b2:1;
int b3:1;
int b4:1;
int b5:1;
int b6:1;
int b7:1;
}
#define BREF(var,bit) ????
#define BAR 4
#define ALIAS BREF(foo,BAR)
unsigned char foo;
such that this statement:
ALIAS = 1;
expands to this:
((struct bits *)&foo)->b4 = 1;
and this:
int k = ALIAS;
to this:
int k = ((struct bits *)&foo)->b4;
So far, this is my implementation of BREF(…):
#define BREF(var,bit) (((struct bits *) &(var))->b##bit)
However, this only works if bit is a literal numeral. I want to be able to pass in a macro variable that expands into a number. How can I make the C preprocessor expand bit before concatenating it to b?
You can use an extra step of expansion, like this: