I did it like this:
Code:
unsigned get_offset(void * f)
{
int * arr = (int*)f;
int i = 0;
while (!arr[i])
i++;
return i * sizeof(int);
}
#define GetOffset(str, field) ({str f; memset(&f, 0, sizeof(f)); f.field = 1; get_offset(&f);})
Called like:
Code:
printf("\nb: %u\n", GetOffset(Foo, b) );
I think that's the first time I've ever used a void* and meant it
I shouldn't have called it GetOffset since that's not what it does.... GetIntSizedBitfieldContainerOffset would be better but depends how much you like typing.
I could have put the whole thing in a macro, but writing many lines of straight code inside a macro always feels wrong to me.