www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - acces to nested struc/unit member

reply novice2 <sorry noem.ail> writes:
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.
Dec 29 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
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
Dec 29 2006
prev sibling parent Derek Parnell <derek psych.ward> writes:
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
Dec 29 2006