Monday 26 June 2017

offset of a field within a structure

The offsetof() macro() does exactly this.

 
#define offsetof(type, mem) 
        ((size_t)((char *)&((type *)0)->mem - (char *)(type *)0))
 

The offset of field a in struct mystruct is


offset_of_a = offsetof(struct mystruct, a)

No comments:

Post a Comment