Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9825

Re: Compliant way to use offsetof for a field within a field?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Compliant way to use offsetof for a field within a field?
Date 2011-09-01 12:23 -0400
Organization A noiseless patient Spider
Message-ID <4E5FB187.7030305@verizon.net> (permalink)
References <fbfb883a-2d39-4aa4-832f-de302fac65c7@j4g2000prh.googlegroups.com>

Show all headers | View raw


On 09/01/2011 11:28 AM, skillzero@gmail.com wrote:
> 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

There's not much point in testing that one: on a conforming
implementation of C, it's guaranteed to be 0.

> 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?

offsetof(Test,fields) should give you the offset of z, for the same
reason that offsetof(Test,x) should give you a 0.

Of course, that answer wouldn't help if "fields" had multiple members.
The solution is to give "fields" a named type:

       struct some_name {
           uint8_t z;
           double d;
       } fields;

Then, instead of testing for offsetof(Test, fields.d), test separately
for offsetof(Test, fields) and offsetof(struct some_name, d).

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Compliant way to use offsetof for a field within a field? "skillzero@gmail.com" <skillzero@gmail.com> - 2011-09-01 08:28 -0700
  Re: Compliant way to use offsetof for a field within a field? James Kuyper <jameskuyper@verizon.net> - 2011-09-01 12:23 -0400
    Re: Compliant way to use offsetof for a field within a field? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-09-02 00:34 +0100
  Re: Compliant way to use offsetof for a field within a field? "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-09-01 10:19 -0700
    Re: Compliant way to use offsetof for a field within a field? Peter Nilsson <airia@acay.com.au> - 2011-09-01 16:31 -0700
  Re: Compliant way to use offsetof for a field within a field? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-09-01 16:38 -0700
  Re: Compliant way to use offsetof for a field within a field? Michael Press <rubrum@pacbell.net> - 2011-09-01 22:05 -0700

csiph-web