Thread overview
acces to nested struc/unit member
Dec 29, 2006
novice2
Dec 29, 2006
Thomas Kuehne
Dec 29, 2006
Derek Parnell
December 29, 2006
Hello. Could please anybody explain me syntax to access to nested structure/union members? In Windows API translated headers thearis situation - anonimous union nested in structure. This is minimal trivial extracted example:

//dmd\html\d\struct.html: "anonymous structs/unions are allowed as members of other structs/unions"

struct Stru
{
  int a;
  union Uni
  {
    int b;
    int c;
  };
}


void main()
{
  Stru s;

  //s.Uni.b = 2; // Error: need 'this' to access member b

  //s.b = 2;     // Error: no property 'b' for type 'Stru'
                 // Error: constant (s).b is not an lvalue
}


How i can access to s.Uni.b ?
Thanks for any advises.
December 29, 2006
novice2 <sorry@noem.ail> schrieb:
> Hello. Could please anybody explain me syntax to access to nested structure/union members? In Windows API translated headers thearis situation - anonimous union nested in structure. This is minimal trivial extracted example:
>
> //dmd\html\d\struct.html: "anonymous structs/unions are allowed as members of other structs/unions"
>
> struct Stru
> {
>   int a;
>   union Uni
>   {
>     int b;
>     int c;
>   };
> }

This union is named "Stru.Uni", it isn't anonymous.

The anonymous version is:
> struct Stru
> {
>   int a;
>   union
>   {
>     int b;
>     int c;
>   };
> }

Thomas
December 29, 2006
On Fri, 29 Dec 2006 14:52:59 +0000 (UTC), novice2 wrote:

> Hello. Could please anybody explain me syntax to access to nested structure/union members? In Windows API translated headers thearis situation - anonimous union nested in structure. This is minimal trivial extracted example:
> 
> //dmd\html\d\struct.html: "anonymous structs/unions are allowed as members of other structs/unions"
> 
> struct Stru
> {
>   int a;
>   union Uni
>   {
>     int b;
>     int c;
>   };
> }
> 
> 
> void main()
> {
>   Stru s;
> 
>   //s.Uni.b = 2; // Error: need 'this' to access member b
> 
>   //s.b = 2;     // Error: no property 'b' for type 'Stru'
>                  // Error: constant (s).b is not an lvalue
> }
> 
> 
> How i can access to s.Uni.b ?
> Thanks for any advises.

Either use anonymous struct or declare and instance of the struct.

Eg. (Anonymous)

 struct Stru
 {
   int a;
   union  // Don't give this a name.
   {
     int b;
     int c;
   };
 }


 void main()
 {
   Stru s;
   s.b = 2;
 }


Eg. (Named)

 struct Stru
 {
   int a;
   union  Uni // Named, therefore must instantiate it.
   {
     int b;
     int c;
   };
   Uni u;
 }


 void main()
 {
   Stru s;
   s.u.b = 2;
 }

-- 
Derek Parnell