Skip to content

Commit 486d167

Browse files
ntreldlang-bot
authored andcommitted
[spec/struct] Document pointer to struct dot operator
List pointer as a valid left operand of dot expression. Add panel explaining access of a member of a struct pointer.
1 parent 73c91e1 commit 486d167

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

spec/expression.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,8 @@ $(TABLE
16131613
* Access a $(DDLINK spec/property, Properties, property) of a type or expression.
16141614
* Access a member of a module, package, aggregate type or instance, enum
16151615
or template instance.
1616+
* Dereference a $(DDSUBLINK spec/struct, struct-pointer, pointer to a struct/union)
1617+
instance and access a member of it.
16161618
* Call a free function using $(DDSUBLINK spec/function, pseudo-member, UFCS).
16171619
)
16181620
$(TROW `.` *NewExpression*, Instantiate a $(DDSUBLINK spec/class, nested-explicit,

spec/struct.dd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ void main()
5959
---
6060
)
6161

62-
$(P For local variables, a struct instance is allocated on the stack
63-
by default. To allocate on the heap, use `new`:)
62+
$(P For local variables, a struct/union instance is allocated on the stack
63+
by default. To allocate on the heap, use $(DDSUBLINK spec/expression, new_expressions,
64+
`new`), which gives a pointer.)
65+
66+
$(PANEL
67+
A $(LNAME2 struct-pointer, pointer to a struct) or union is automatically
68+
dereferenced when using the `.` operator to access members.
6469
---
6570
S* p = new S;
66-
assert(p.i == 0);
71+
assert(p.i == 0); // `p.i` is the same as `(*p).i`
6772
---
73+
$(NOTE There is no `->` operator as in C.)
74+
)
6875

6976
$(P A struct can contain multiple fields which are stored sequentially.
7077
Conversely, multiple fields in a union use overlapping storage.)

0 commit comments

Comments
 (0)