Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Struct and Union |
| Date | Fri, 10 Jul 2015 12:38:26 -0700 |
| Organization | None to speak of |
| Lines | 49 |
| Message-ID | <lntwtbrefh.fsf@kst-u.example.com> (permalink) |
| References | <a653fc98-5fae-48ff-ba1c-d7b53adee150@googlegroups.com> <405a385f-f728-45c0-adfa-c6e1a1662938@googlegroups.com> <87oajkvukx.fsf@bsb.me.uk> <44898eb2-560d-444f-bea4-1ddf7bb69f36@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="26613"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19S+j+XfMu6JypbOT4KqKmf" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:cTBgqXs8cKfKlBt3ZTaU6TPVyRo= sha1:UhYOAawF8gh+EIODQtoP2niUfzo= |
| Xref | csiph.com comp.lang.c:65182 |
Show key headers only | View raw
David Kleinecke <dkleinecke@gmail.com> writes:
> On Friday, July 10, 2015 at 9:36:07 AM UTC-7, Ben Bacarisse wrote:
>> "Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes:
>> > On Friday, July 10, 2015 at 3:49:56 AM UTC-4, janus wrote:
>> >> I have 2 options but which one is idiomatic C?
>> >>
>> >> 1)
>> >> typedef struct CStatement
>> >> {
>> >> int stype;
>> >>
>> >> union Statement
>> >> {
>> >> CPrintStatement* printstatement;
>> >> CInputStatement* inputstatement;
>> >> CAssignStatement* assignstatement;
>> >> CGotoStatement* gotostatement;
>> >> CIfThenStatement* ifthenstatement;
>> >> void* sstatement;
>> >>
>> >> } statement;
>> >> }CStatement;
>
> The real reason for using a union is if the members are of different
> sized types and you want to insure that all of them fit.
That's not the only reason. A union exposes the fact that only one of
the members is valid at any time. (You need other information, in this
case `stype`, to keep track of which one is currently valid.) Using a
union rather than a structure saves some space (not a lot in this case),
but that space saving is not the only reason to use a union.
> All pointers
> are the same size so a union is inappropriate here.
All pointers are *not* necessarily the same size. And even if they are
for a given implementation (which is common), that wouldn't be a good
argument against using a union; the pointers are of different *types*,
even if they have exactly the same representation.
Given that they're pointers, using a single void* is also a reasonable
approach. Again we don't necessarily know (and needn't care) whether
void* and CPrintStatement* are the same size.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Struct and Union janus <emekamicro@gmail.com> - 2015-07-10 00:49 -0700
Re: Struct and Union Richard Heathfield <rjh@cpax.org.uk> - 2015-07-10 09:04 +0100
Re: Struct and Union janus <emekamicro@gmail.com> - 2015-07-10 01:40 -0700
Re: Struct and Union Richard Heathfield <rjh@cpax.org.uk> - 2015-07-10 09:50 +0100
Re: Struct and Union Richard Heathfield <rjh@cpax.org.uk> - 2015-07-10 13:44 +0100
Re: Struct and Union "Bill Davy" <Bill@SynectixLtd.com> - 2015-07-10 12:37 +0100
Re: Struct and Union Keith Thompson <kst-u@mib.org> - 2015-07-10 08:24 -0700
Re: Struct and Union Keith Thompson <kst-u@mib.org> - 2015-07-12 12:53 -0700
Re: Struct and Union Ian Collins <ian-news@hotmail.com> - 2015-07-10 21:32 +1200
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 05:12 -0700
Re: Struct and Union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 17:35 +0100
Re: Struct and Union David Kleinecke <dkleinecke@gmail.com> - 2015-07-10 10:18 -0700
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 10:23 -0700
Re: Struct and Union James Kuyper <jameskuyper@verizon.net> - 2015-07-10 13:47 -0400
Re: Struct and Union Keith Thompson <kst-u@mib.org> - 2015-07-10 12:38 -0700
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 10:27 -0700
Re: Struct and Union Bartc <bc@freeuk.com> - 2015-07-10 13:55 +0100
Re: Struct and Union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 21:48 +0100
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 13:51 -0700
Re: Struct and Union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 23:45 +0100
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 17:43 -0700
Re: Struct and Union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 13:04 +0100
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-11 05:15 -0700
Re: Struct and Union Bartc <bc@freeuk.com> - 2015-07-11 14:01 +0100
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-11 06:05 -0700
Re: Struct and Union "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-11 06:13 -0700
Re: Struct and Union Keith Thompson <kst-u@mib.org> - 2015-07-11 13:51 -0700
Re: Struct and Union Martin Shobe <martin.shobe@yahoo.com> - 2015-07-11 16:51 -0500
Re: Struct and Union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 23:13 +0100
Re: Struct and Union Keith Thompson <kst-u@mib.org> - 2015-07-11 17:39 -0700
Re: Struct and Union Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-11 16:56 -0700
Re: Struct and Union Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-10 18:52 -0700
Re: Struct and Union Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-10 19:12 -0700
csiph-web