"Need 'this' to access structure member"

Derek Parnell derek at nomail.afraid.org
Thu Jun 22 23:35:23 PDT 2006


On Fri, 23 Jun 2006 05:59:50 +0000 (UTC), alxdef wrote:

> Hi there!
> I am beginner in D and I confused with subject.
> Here my source:
> 
> Module some_mod.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> module some_mod;
> struct struct1
> {
> union addr
> {
> struct
> {
> ushort offs;
> ushort seg;
> }
> uint flat;
> };
> };
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In main.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> import some_mod;
> struct1 s;
> s.addr.flat = 0;
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Compile command: DMD main.d some_mod.d
> 
> At this point compiler says the subject message. When I make 'offs', 'seg', and
> 'flat' as static compilation is well, but it is not what I need, cause fields of
> union is unmodifiable. Could someone help with this?
> Thanks a lot!

You have two choices.

(1) Choice #1.
 Module some_mod.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 module some_mod;
 struct struct1
 {
 union addr
 {
 struct
 {
 ushort offs;
 ushort seg;
 }
 uint flat;
 };
 addr a;
 };
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 In main.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 import some_mod;
 struct1 s;
 s.a.flat = 0;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(2) Choice #2.
 Module some_mod.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 module some_mod;
 struct struct1
 {
 union
 {
 struct
 {
 ushort offs;
 ushort seg;
 }
 uint flat;
 };
 };
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 In main.d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 import some_mod;
 struct1 s;
 s.flat = 0;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Now to explain...
The first choice uses a named Union. You called it 'addr'. But in D, can't
define a named union (or struct) and declare an instance of it in the same
statement. You have to use two statements ... for example:

   // Define the named union.
   union somename
   {
      int a;
      long b;
   }
   // Declare an instance of it.
   somename foo;

Once you have an instance of it, you can use it to access the union members
...

   foo.a = 1;

The second choice uses an unnamed (anonymous) union. In this case, when you
define a anonymous union you cannot declare an instance of separately
(because it doesn't have a name) so the definition also serves as the
declaration. You then access its members directly, again because there is
no instance name you can't use it.

   // Define the anonymous union.
   union 
   {
      int a;
      long b;
   }

Once you have an instance of it, you can use it to access the union members
...

   a = 1;

*But* you can only define anonymous unions and structs if there are inside
a union or struct. So the top-level container must have a name.


Anyhow, here your example reworked ...

---------some_mod.d -------------
module some_mod;
struct struct1
{
    union
    {
        struct
        {
            ushort offs;
            ushort segm;
        }
        uint flat;
    }
}
---------------------------------

----------- main.d ---------------
import some_mod;
import std.stdio;
void main()
{
    struct1 s;
    s.flat = 0xFFEEDDCC;
    writefln("%x %x", s.offs, s.segm);
}
---------------------------------

This displays "ddcc ffee" when run.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
23/06/2006 4:21:25 PM



More information about the Digitalmars-d mailing list