S
skillzero
Is there a compliant way of getting the offset of a field within a
field? I'm mainly using this to do compile-time checks that fields are
at the offset I expect them. I'm using offsetof( NameOfStruct,
field1.field2 ) now and this works, but with some compilers, I'm
getting a warning about "Using extended field designator is an
extension". Here's an example:
typedef struct
{
uint8_t x;
uint8_t y;
struct
{
uint8_t z;
} fields;
} Test;
compile_time_assert( offsetof( Test, x ) == 0 ); // Works
compile_time_assert( offsetof( Test, y ) == 1 ); // Works
compile_time_assert( offsetof( Test, fields.z ) == 2 ); // Works, but
warns about an extension.
Is there a way to do this in a compliant way?
field? I'm mainly using this to do compile-time checks that fields are
at the offset I expect them. I'm using offsetof( NameOfStruct,
field1.field2 ) now and this works, but with some compilers, I'm
getting a warning about "Using extended field designator is an
extension". Here's an example:
typedef struct
{
uint8_t x;
uint8_t y;
struct
{
uint8_t z;
} fields;
} Test;
compile_time_assert( offsetof( Test, x ) == 0 ); // Works
compile_time_assert( offsetof( Test, y ) == 1 ); // Works
compile_time_assert( offsetof( Test, fields.z ) == 2 ); // Works, but
warns about an extension.
Is there a way to do this in a compliant way?