I read on cppreference (https://en.cppreference.com/w/c/types/offsetof):
Even though it is specified in C23 that defining a new type in offsetof is undefined behavior, such usage is only partially supported by implementations even in earlier modes:
offsetof(struct Foo { int a; }, a)
is usually supported, butoffsetof(struct Foo { int a, b; }, a)
is not because of the comma in the definition of struct Foo.
So it won't work with new types because of the commas in the definitions, does that mean using typeof
will make it work (since it'll wrap the type in parentheses)?
offsetof(typeof(struct Foo { int a,b; }), a);
edit:
if this doesn't work, is there a workaround to have the struct be defined inside the offsetof
?
for example:
void *dummy;
offsetof(typeof(*((struct { int a; int b; }*)dummy)), a);
C 2023 draft N3096 7.21 4 says, of
offsetof(type, member-designator)
:Therefore the C standard does not require
offsetof
to work with a new type in a conforming C implementation, regardless of whether or not the macro argument fortype
contains a comma. If the argument contains atypeof
in which a new type is defined, the argument defines a new type and is subject to the above.Another reason it might not work with a new type, hypothetically, is the macro might use the type argument in its replacement list in a way that causes problems due to multiple definitions of the type. It is not a correct logical inference that because X (presence of a comma) causes Y (
offsetof
will not work) that Not X will cause Not Y.