[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] [PATCH] Parentheses handling within struct/union init
From: |
Dave Dodge |
Subject: |
Re: [Tinycc-devel] [PATCH] Parentheses handling within struct/union initialization |
Date: |
Fri, 28 Sep 2007 14:02:59 -0400 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
On Fri, Sep 28, 2007 at 06:47:09PM +0200, Marc Andre Tanner wrote:
> struct in6_addr {
> union {
> uint8_t u6_addr8 [ 16 ] ;
> uint16_t u6_addr16 [ 8 ] ;
> uint32_t u6_addr32 [ 4 ] ;
> } in6_u ;
> } ;
[...]
> IPADDR ip4_any = {
> 2,
> { ( ( in_addr_t ) 0x00000000 ) } // broken
> // { ( in_addr_t ) 0x00000000 } // this would work
> };
Two things to note about this:
- It's making use of a gcc language extension. Proper C would require
at least some additional braces around that initializer value.
- Even with gcc's initializer extension, it only produces the
"correct" result as a side effect. The lack of a designated
initializer means that the initializer value gets assigned only to
the very first thing in the union. The 32-bit (in_addr_t)0 is
truncated to 8 bits and assigned to u6_addr8[0]. The remaining 15
elements of u6_addr8[] are implicitly zero-filled just like any
other integer array that only had its first element initialized.
The cast to in_addr_t is misleading, because it's not going to store
32 bits in the union and if you try to use any value _other_ than 0
you may not get what you want. For example if you try something
like 0xffffffff it will assign 0xff to only the first byte of the
union and zero-fill the other 15.
-Dave Dodge