Consider the following C code:
struct Foo
{
short a;
long b;
char c;
void* d;
};
I know it is possible to know the size of the entire structure using sizeof. Is it possible to know the size of a subset of this structure, or (stated differently) know the distance between two members? I realize could nest structures based on the offsets I want (then it is possible to use sizeof), but say I wanted to know the number of bytes between a and d? Also, I’d like to do this without instantiating an object (something like sizeof). (I realize if I have a object, then I could look at the address differences between foo.d and foo.a). Is this possible?
offsetofgives you the offset from the beginning ofFooto the member specified.You’ll need
#include <stddef.h>