I want the offsetof() the param line in mystruct1. I’ve tried
offsetof(struct mystruct1, rec.structPtr1.u_line.line)
and also
offsetof(struct mystruct1, line)
but neither works.
union {
struct mystruct1 structPtr1;
struct mystruct2 structPtr2;
} rec;
typedef struct mystruct1 {
union {
struct {
short len;
char buf[2];
} line;
struct {
short len;
} logo;
} u_line;
};
The
offsetof()macro takes two arguments. The C99 standard says (in §7.17<stddef.h>):So, you need to write:
However, we can observe that the answer will be zero since
mystruct1contains aunionas the first member (and only), and thelinepart of it is one element of the union, so it will be at offset 0.