nested unions and structs

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Apr 27 14:20:51 PDT 2006


Alberto Simon wrote:
> It looks like I didn't explain myself. I expect that when I declare a DWord
> union and try to access its fields, Data should have the same address LowerWord
> has and the same address Low on LowerWord has, but that does not happen, Data
> instead is 4 bytes after LowerWord, and that ticks me, since all fields of an
> union should start at the same address.

No, that is not correct. &Dword.LowerWord should be +4 bytes from 
&DWord.Data. The anonymous struct makes sure of that.

/Oskar


> 
> In article <e2q4a7$ljq$1 at digitaldaemon.com>, Oskar Linde says...
>> Alberto Simon wrote:
>>> The union DWord below does not product the expected union, Data isn't at the
>>> same address as the union start address is... What should I do to correct that??
>>>
>>> public union Word
>>> {
>>> // Atributos
>>>
>>> struct
>>> {
>>> public ubyte Low;
>>> public ubyte High;
>>> }
>>> public ushort Data;
>>> }
>>>
>>> public union DWord
>>> {
>>> // Atributos
>>>
>>> public uint Data;
>>> struct
>>> {
>>> public Word HigherWord;
>>> public Word LowerWord;
>>> };
>>> }
>> On what platform & compiler? On Linux dmd 0.154 they are for me:
>>
>>   Word w;
>>   DWord d;
>>   writefln("%s %s",&d,&(d.Data));
>>   writefln("%s %s",&w,&(w.Data));
>>
>> Prints:
>>
>> bfffed24 bfffed24
>> bfffed20 bfffed20
>>
>> /Oskar
> 
> Regards,
> Alberto Simon (20y student)



More information about the Digitalmars-d mailing list