Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: C struct compatibility when they have the same initial members |
| Date | 2015-05-14 11:22 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87oaln4fxh.fsf@bsb.me.uk> (permalink) |
| References | <miv94g$qme$1@dont-email.me> <mj0u18$t0k$1@reader1.panix.com> <mj1i0i$i2p$1@dont-email.me> |
"James Harris" <james.harris.1@gmail.com> writes:
> "Pierre Asselin" <pa@see.signature.invalid> wrote in message
> news:mj0u18$t0k$1@reader1.panix.com...
>> James Harris <james.harris.1@gmail.com> wrote:
>>> [ ... ]
>>> 2. Define a struct which has just the pointers and embed that at the
>>> start of each of the other types as in
>>
>>> struct common {
>>> struct common *next;
>>> struct common *prev;
>>> );
>>
>>> struct specific1 {
>>> struct common c;
>>> <rest of this node type>
>>> };
>>
>>> Again, I don't think this will work without casts.
>
>> I think it does work without a cast, but you have to name the
>> intermediate field, e.g. ptr->c.next, ptr->c.prev instead of just
>> ptr->next, ptr->prev.
>
>
> If I define the head and tail nodes as
>
> struct common head, tail;
>
> and define a normal node, using the above name, as
>
> struct specific1 node;
>
> Then it looks as though head.next will have the same type as
> node.c.prev, for example.
You'd still need a cast to treat the struct common * you get as a
pointer to one of the specif types you might need to use (and you still
have potential alignment and aliasing issues).
If you put all the types into a union you can have the next and prev
pointers be to that union type which make for simple cast-free accesses
(though you do got a lot of member names -- macros can help with that).
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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