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


Groups > comp.lang.c > #9978

Re: Another Union question

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Another Union question
Date 2011-09-06 18:33 -0700
Organization None to speak of
Message-ID <lnhb4pp25c.fsf@nuthaus.mib.org> (permalink)
References <j461mk$pcu$1@speranza.aioe.org> <4E669424.6070908@verizon.net>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
> On 09/06/2011 05:01 PM, Quentin Pope wrote:
[...]
>> Now I have an union
>> union r32{
>>    uint8       l;
>>    uint16      x;
>>    uint32      val;
[...]
>> };
>> 
>> union  r32   reg;
>> 
>> and a function
>> void   f(union r32*  a){a->x=1;}
>> 
>> then
>> 
>> is to do   f(&reg.x) or f(&reg.l) or f(&reg.val) OK or not?
>
> With a function prototype in scope, all of those pointers are implicitly
> converted to union r32* when passed to that function. The result of that
> conversion is guaranteed to point at the containing union, so all three
> are OK.
[...]

There is no implicit conversion from a pointer-to-integer type to a
pointer-to-union type.  An explicit conversion (a cast) should work ok:

    f((union r32*)&reg.x);
    f((union r32*)&reg.x);
    f((union r32*)&reg.val);

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"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 | Find similar | Unroll thread


Thread

Another Union question Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-09-06 21:01 +0000
  Re: Another Union question James Kuyper <jameskuyper@verizon.net> - 2011-09-06 17:44 -0400
    Re: Another Union question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-07 02:08 +0100
      Re: Another Union question James Kuyper <jameskuyper@verizon.net> - 2011-09-07 08:04 -0400
    Re: Another Union question Keith Thompson <kst-u@mib.org> - 2011-09-06 18:33 -0700

csiph-web