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


Groups > comp.lang.c > #9982

Re: Another Union question

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Another Union question
Date 2011-09-07 08:04 -0400
Organization A noiseless patient Spider
Message-ID <j47mkc$klr$1@dont-email.me> (permalink)
References <j461mk$pcu$1@speranza.aioe.org> <4E669424.6070908@verizon.net> <0.0c8797be815ceed52054.20110907020845BST.87vct540rm.fsf@bsb.me.uk>

Show all headers | View raw


On 09/06/2011 09:08 PM, Ben Bacarisse wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
> 
>> On 09/06/2011 05:01 PM, Quentin Pope wrote:
> <snip>
>>> Now I have an union
>>> union r32{
>>>    uint8       l;
>>>    uint16      x;
>>>    uint32      val;
> <snip>
>>> 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.
> 
> That's not right.  It's a constraint violation if an argument has a type
> that means it could not be assigned to an object with the (unqualified)
> version of the parameter type.

You're right - I don't know how I managed to forget that. It is
therefore not merely pointless, but just plain wrong, to use any of
those expressions.

f((union r32*)&reg.x) would, however, be a perfectly fine, though
pointlessly complicated, way of writing f(&reg). If taken as short-hand
for something that happens in two different stages in widely separated
scopes, it actually becomes more reasonable:

	void g(uint16 *pui16)
	{
		f((union r32*)pui16));
	}

In some other function:

	g(&reg.x);

-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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