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


Groups > comp.lang.c > #65101

Re: Struct and Union

From "Bill Davy" <Bill@SynectixLtd.com>
Newsgroups comp.lang.c
Subject Re: Struct and Union
Date 2015-07-10 12:37 +0100
Message-ID <d09p4cFjht7U1@mid.individual.net> (permalink)
References <a653fc98-5fae-48ff-ba1c-d7b53adee150@googlegroups.com> <mnnu72$59k$1@dont-email.me>

Show all headers | View raw


"Richard Heathfield" <rjh@cpax.org.uk> wrote in message 
news:mnnu72$59k$1@dont-email.me...
> On 10/07/15 08:49, janus wrote:
>> Hello All,
>>
>> 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;
>>
>>
>> 2)
>> typedef struct CStatement
>> {
>>      int stype;
>>
>>      void* sstatement;
>>
>> }CStatement;
>>
>
>
> To get your actual question out of the way, I would argue that the second 
> is idiomatic C, whereas the first looks inelegant and crufty.
>
> If it weren't for your catch-all void * in the first one, I'd say it was 
> marginally safer, in that adding an extra type in one part of the code and 
> forgetting to support it everywhere else is more likely to bite you at 
> compile time rather than at runtime (which is a Good Thing), but the 
> catch-all void * somewhat diminishes that advantage.
>
> Which would I choose? The second one, without question. But I'd tread 
> carefully.
>
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vaca

Is enum part of C?  If so, I'd prefer enum {tPrint=1, tInput, tGoto, etc} 
stype (or whatever the syntax is).

This makes it easier for a compiler (or checker like PC-Lint) to see if a 
switch on stype is missing a value, which is useful when the list changes 
later.  Avoiding the enumeration value zero also slightly improves your odds 
in that a null initialised stype is incorrect. 

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


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