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


Groups > comp.lang.c > #61966

Re: C struct compatibility when they have the same initial members

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: C struct compatibility when they have the same initial members
Date 2015-05-14 14:33 +1200
Message-ID <crifr5FnbhrU1@mid.individual.net> (permalink)
References <miv94g$qme$1@dont-email.me> <mivtsj$8bt$1@dont-email.me>

Show all headers | View raw


James Kuyper wrote:
> On 05/13/2015 06:31 AM, James Harris wrote:
>> The basic query is: If two or more struct types begin with the same
>> types of members is it possible to treat a struct which has just the
>> common part as compatible with the others without the use of casts?
>
> ....
>> 3. Define a union or unions such that the common struct type (the one
>> with just the two pointers) is unioned with each of the other struct
>> types. I see in C89 "3.3.2.3 Structure and union members" that this
>> should make them compatible but it seems complex and even if it works I
>> am not sure that it is a good way to go.
>
> In C2011, the relevant clause is 6.5.2.3p6. I don't have a copy of C89,
> so I can't be sure, but I don't think substantial changes have been made
> to this clause:
>
>> One special guarantee is made in order to simplify the use of unions:
>> if a union contains several structures that share a common initial
>> sequence (see below), and if the union object currently contains one
>> of these structures, it is permitted to inspect the common initial
>> part of any of them anywhere that a declaration of the completed type
>> of the union is visible. Two structures share a common initial
>> sequence if corresponding members have compatible types (and, for
>> bit-fields, the same widths) for a sequence of one or more initial
>> members
>
> A couple of key points that you need to be aware of:
> 1. You must actually declare a union containing the structures you're
> talking, and that declaration must be in scope at the point where you
> rely upon this guarantee.
> 2. The guarantee applies only if there's an actual union object
> containing the struct object you're working on.

I doubt that there have been changes, lots of well known code (X for 
example) has relied on this behaviour for decades!

-- 
Ian Collins

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


Thread

C struct compatibility when they have the same initial members "James Harris" <james.harris.1@gmail.com> - 2015-05-13 11:31 +0100
  Re: C struct compatibility when they have the same initial members Johann Klammer <klammerj@NOSPAM.a1.net> - 2015-05-13 17:17 +0200
  Re: C struct compatibility when they have the same initial members James Kuyper <jameskuyper@verizon.net> - 2015-05-13 12:25 -0400
    Re: C struct compatibility when they have the same initial members Ian Collins <ian-news@hotmail.com> - 2015-05-14 14:33 +1200
    Re: C struct compatibility when they have the same initial members "James Harris" <james.harris.1@gmail.com> - 2015-05-14 08:04 +0100
      Re: C struct compatibility when they have the same initial members James Kuyper <jameskuyper@verizon.net> - 2015-05-15 00:19 -0400
        Re: C struct compatibility when they have the same initial members "James Harris" <james.harris.1@gmail.com> - 2015-05-15 21:32 +0100
          Re: C struct compatibility when they have the same initial members James Kuyper <jameskuyper@verizon.net> - 2015-05-15 17:11 -0400
            Re: C struct compatibility when they have the same initial members Keith Thompson <kst-u@mib.org> - 2015-05-15 15:27 -0700
  Re: C struct compatibility when they have the same initial members Barry Schwarz <schwarzb@dqel.com> - 2015-05-13 11:22 -0700
  Re: C struct compatibility when they have the same initial members pa@see.signature.invalid (Pierre Asselin) - 2015-05-14 01:33 +0000
    Re: C struct compatibility when they have the same initial members "James Harris" <james.harris.1@gmail.com> - 2015-05-14 08:15 +0100
      Re: C struct compatibility when they have the same initial members Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-14 11:22 +0100
        Re: C struct compatibility when they have the same initial members Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-14 11:27 +0100

csiph-web