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


Groups > comp.lang.c > #14351

Re: Struct casting

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Struct casting
Date 2011-12-06 22:30 +1300
Message-ID <9k65ksFuhiU2@mid.individual.net> (permalink)
References <jbjeed$c1v$1@speranza.aioe.org> <jbkg83$ad8$1@speranza.aioe.org> <bebc7ea8-a1f8-48e9-b58c-dc669f6868bc@d10g2000vbf.googlegroups.com>

Show all headers | View raw


On 12/ 6/11 09:15 PM, Malcolm McLean wrote:
> On Dec 6, 9:29 am, jacob navia<ja...@spamsink.net>  wrote:
>>
>> int * i = (int *) f; // 20 characters
>>
>> I see no justification whatsoever for writing code
>> like that.
>>
> As the snippet goes, no.
>
> But "bar" might in fact be type a identifier field
>
> foodoo(struct foo *f)
> {
>    switch( *( int *) f)
>    {
>       case REALLYABAR:
>         dobar(f); return;
>       case REALLYAFOO:
>         dofoo(f); return;
>    }
> }
>
> This is arguably better than switch(foo->bar) because it makes clear
> that code will onyl work if the identifier is the first member.

Better still is to give the first member a name that identifies is as 
the type identifier field.  The X XEvent union is a good example of this.

-- 
Ian Collins

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


Thread

Struct casting Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-05 21:52 +0000
  Re: Struct casting Harald van Dijk <truedfx@gmail.com> - 2011-12-05 13:58 -0800
  Re: Struct casting Tobias Blass <tobiasblass@gmx.net> - 2011-12-05 22:00 +0000
    Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:10 +1300
  Re: Struct casting tom st denis <tom@iahu.ca> - 2011-12-05 14:06 -0800
    Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:11 +1300
    Re: Struct casting James Kuyper <jameskuyper@verizon.net> - 2011-12-05 17:13 -0500
  Re: Struct casting Robert Wessel <robertwessel2@yahoo.com> - 2011-12-05 16:10 -0600
    Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 11:13 +1300
    Re: Struct casting James Kuyper <jameskuyper@verizon.net> - 2011-12-05 17:15 -0500
  Re: Struct casting Keith Thompson <kst-u@mib.org> - 2011-12-05 14:16 -0800
    Re: Struct casting Lauri Alanko <la@iki.fi> - 2011-12-06 18:00 +0000
  Re: Struct casting blp@cs.stanford.edu (Ben Pfaff) - 2011-12-05 14:19 -0800
  Re: Struct casting jacob navia <jacob@spamsink.net> - 2011-12-06 08:29 +0100
    Re: Struct casting Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-06 00:15 -0800
      Re: Struct casting Ian Collins <ian-news@hotmail.com> - 2011-12-06 22:30 +1300
  Re: Struct casting jgk@panix.com (Joe keane) - 2011-12-08 21:08 +0000

csiph-web