i interested in generating array offset of structure fields.
struct b { int pig[100]; bool donkey[100]; } struct { int ant[100]; bool cat[38]; struct b dog[78]; } main () { int offset[100+38+78] /* ant offsets + cat offsets + dog offsets */ /* how fill these offset array fill in offsets of ant, cat, dog */ } output should like: offset[0] = 0; offset[1] = 4; ...... ...... offset[99]= 4*99; ....... offset[100+38+78-1] = ? ;
i know c doesn't support reflection , can make use of x_macros extent, structures complex. kickstart basic simple structure had posted in question.
you can convert addresses of array elements char *
, subtract address of structure.
struct a; (int = 0; < 100; i++) { offset[i] = (char*)&a.ant[i] - (char*)&a; } (int = 0; < 38; i++) { offset[100 + i] = (char*)&a.cat[i] - (char*)&a; } (int = 0; < 78; i++) { offset[100 + 38 + i] = (char*)&a.dog[i] - (char*)&a;
Comments
Post a Comment