i have following piece of code , want programmatically access fields of struct:
#include <stdio.h> #include <stddef.h> #define field(name, num) {name##num} struct sfoo { int i; float f1; float f2; char c; }; int main() { printf("offset of 'f1' %u", offsetof(sfoo, f1)); printf("offset of 'f1' %u", offsetof(sfoo, field(f, 1))); }
the first line in main()
works second line doesn't compile (msvc 2012). how can generate field name using token concatenation?
get rid of {
, }
in definition of field
.
#define field(name, num) name##num
with previous definition, print statement expand offsetof
macro won't accept:
printf("offset of 'f1' %u", offsetof(sfoo, {f1}));
Comments
Post a Comment