@@ -4,14 +4,14 @@ $(SPEC_S Structs and Unions,
44
55$(HEADERNAV_TOC)
66
7- $(H2 $(LNAME2 intro, Introduction ))
7+ $(H2 $(LNAME2 intro, Overview ))
88
99 $(P Whereas $(DDLINK spec/class, Classes, classes) are reference types,
1010 structs and unions are value types.
11- Structs are simple aggregations of data and their
12- associated operations on that data.
1311 )
1412
13+ $(H3 $(LNAME2 structs, Structs))
14+
1515$(GRAMMAR
1616$(GNAME StructDeclaration):
1717 $(D struct) $(GLINK_LEX Identifier) $(D ;)
@@ -23,21 +23,16 @@ $(GNAME AnonStructDeclaration):
2323 $(D struct) $(GLINK AggregateBody)
2424)
2525$(GRAMMAR
26- $(GNAME UnionDeclaration):
27- $(D union) $(GLINK_LEX Identifier) $(D ;)
28- $(D union) $(GLINK_LEX Identifier) $(GLINK AggregateBody)
29- $(GLINK2 template, UnionTemplateDeclaration)
30- $(I AnonUnionDeclaration)
31-
32- $(GNAME AnonUnionDeclaration):
33- $(D union) $(GLINK AggregateBody)
34- )
35- $(GRAMMAR
3626$(GNAME AggregateBody):
3727 $(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
3828)
3929
40- $(P The following example declares a struct type with a single integer field:)
30+ $(P Structs are simple aggregations of data and their
31+ associated operations on that data.)
32+
33+ $(P The non-static data members of a struct are called $(I fields).
34+ Struct instance members are accessed with the `.` operator.
35+ The following example declares a struct type with a single integer field:)
4136
4237$(SPEC_RUNNABLE_EXAMPLE_RUN
4338---
@@ -48,7 +43,7 @@ struct S
4843
4944void main()
5045{
51- S a;
46+ S a; // declare struct instance
5247 a.i = 3;
5348
5449 S b = a; // copy a
@@ -58,6 +53,10 @@ void main()
5853}
5954---
6055)
56+ $(P Assigning (or initializing) a struct instance from an
57+ $(DDSUBLINK spec/expression, .define-lvalue, lvalue) will copy the original struct.)
58+
59+ $(H3 $(LNAME2 storage, Storage))
6160
6261 $(P For local variables, a struct/union instance is allocated on the stack
6362 by default. To allocate on the heap, use $(DDSUBLINK spec/expression, new_expressions,
@@ -66,13 +65,35 @@ void main()
6665 $(PANEL
6766 A $(LNAME2 struct-pointer, pointer to a struct) or union is automatically
6867 dereferenced when using the `.` operator to access members.
68+
69+ $(SPEC_RUNNABLE_EXAMPLE_RUN
6970 ---
71+ struct S { int i; }
72+
7073 S* p = new S;
71- assert(p.i == 0); // `p.i` is the same as `(*p).i`
74+ S* q = p;
75+
76+ p.i = 2; // `p.i` is the same as `(*p).i`
77+ assert(q.i == 2); // q points to the same struct instance as p
7278 ---
79+ )
7380 $(NOTE There is no `->` operator as in C.)
7481 )
7582
83+ $(P **See also:** $(RELATIVE_LINK2 struct_layout, Struct Layout).)
84+
85+ $(H3 $(LNAME2 unions, Unions))
86+
87+ $(GRAMMAR
88+ $(GNAME UnionDeclaration):
89+ $(D union) $(GLINK_LEX Identifier) $(D ;)
90+ $(D union) $(GLINK_LEX Identifier) $(GLINK AggregateBody)
91+ $(GLINK2 template, UnionTemplateDeclaration)
92+ $(I AnonUnionDeclaration)
93+
94+ $(GNAME AnonUnionDeclaration):
95+ $(D union) $(GLINK AggregateBody)
96+ )
7697 $(P A struct can contain multiple fields which are stored sequentially.
7798 Conversely, multiple fields in a union use overlapping storage.)
7899
@@ -169,8 +190,8 @@ $(H3 $(LNAME2 recursive-types, Recursive Structs and Unions))
169190
170191$(H2 $(LNAME2 struct_layout, Struct Layout))
171192
172- $(P The non-static data members of a struct are called $(I fields). Fields are laid
173- out in lexical order. Fields are aligned according to the $(DDSUBLINK spec/attribute, align, Align Attribute)
193+ $(P Fields are laid out in lexical order.
194+ Fields are aligned according to the $(DDSUBLINK spec/attribute, align, Align Attribute)
174195 in effect.
175196 Unnamed padding is inserted between fields to align fields. There is no padding between
176197 the first field and the start of the object.
0 commit comments