acces to nested struc/unit member
Derek Parnell
derek at psych.ward
Fri Dec 29 15:20:13 PST 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
More information about the Digitalmars-d-learn
mailing list