Structures and pointers
Structures
A structure groups fields under one name.
Structure Point
x.i
y.i
EndStructure
OpenConsole()
Define p.Point
p\x = 3
p\y = 4
PrintN(Str(p\x * p\x + p\y * p\y))
Output:
25
A field needs a type, except for two kinds whose name already says it:
name$ is a string field and *name is a pointer field.
Structure Entry
name$
*link
count.i
EndStructure
OpenConsole()
Define e.Entry
e\name$ = "alpha"
e\count = 2
e\link = 0
PrintN(e\name$ + " " + Str(e\count) + " " + Str(SizeOf(Entry)))
Output:
alpha 2 24
Fields are reached with \. . and -> also work, so p.x and C's p->x mean the same thing.
C style:
struct Point {
int x
int y
}
Point p
p.x = 3
Copying
= copies a whole structure. Strings inside are shared, not duplicated, and each copy behaves as if it had its own:
Structure Person
name.s
age.i
EndStructure
Define a.Person, b.Person
a\name = "Ada"
a\age = 36
b = a
b\name + " Lovelace"
OpenConsole()
PrintN(a\name + " / " + b\name + " " + Str(b\age))
Output:
Ada / Ada Lovelace 36
Layout
Structurepacks fields one after another with no padding, as PureBasic does.structlays fields out the way C does, aligning each one to its size. Use it to match structures that C code or the operating system expects.Align nafter the name aligns every field ton.
Structure Packed
a.b
b.l
EndStructure
struct Aligned {
byte a
long b
}
OpenConsole()
PrintN(Str(SizeOf(Packed)) + " " + Str(SizeOf(Aligned)))
Output:
5 8
Extends adds another structure's fields at the start:
Structure Point3D Extends Point
z.i
EndStructure
Fixed-size arrays in structures
field.type[N] holds exactly N elements, numbered 0 to N − 1:
Structure Samples
count.i
values.l[4]
EndStructure
OpenConsole()
Define s.Samples
For i = 0 To 3
s\values[i] = i * 10
Next
PrintN(Str(s\values[3]) + " " + Str(SizeOf(Samples)))
Output:
30 24
Arrays, lists and maps in structures ✅
A field can be a whole array, list or map:
Array name.type(size [, size2])
List name.type()
Map name.type([slots])
They are made with the structure: a variable, an array element, a list or map element or a local all start with an empty list and map, and an array of size + 1 elements, as Dim would make. They are used like any array, list or map, with the structure in front: s\items(), *p\grid(i), ForEach s\items(), ListSize(s\items()). Dim and ReDim work on an array field: ReDim s\grid(20).
Structure Player
name$
List scores.i()
Map items.i()
Array best.i(2)
EndStructure
OpenConsole()
Define p.Player
p\name$ = "Ann"
AddElement(p\scores()) : p\scores() = 10
AddElement(p\scores()) : p\scores() = 25
p\items("sword") = 1
p\items("potion") = 3
p\best(0) = 25
total = 0
ForEach p\scores()
total + p\scores()
Next
PrintN(p\name$ + " " + Str(ListSize(p\scores())) + " " + Str(total) + " " + Str(p\items("potion")) + " " + Str(MapSize(p\items())))
PrintN(Str(ArraySize(p\best())) + " " + Str(p\best(0)))
Output:
Ann 2 35 3 2
2 25
A structure owns what is in it. Copying a structure, by assignment or with CopyStructure, copies its arrays, lists and maps too, so the copy can be changed on its own. When the structure goes away (a local at the end of its procedure, an element that is deleted, an array that is freed), they are freed with it. A structure can even hold a list of its own type (List children.Node()), which makes a tree.
A list of teams, each with its own list of members, and a copy of one team:
Structure Team
name$
List members.s()
EndStructure
OpenConsole()
NewList teams.Team()
AddElement(teams()) : teams()\name$ = "red"
AddElement(teams()\members()) : teams()\members() = "Ann"
AddElement(teams()\members()) : teams()\members() = "Bob"
AddElement(teams()) : teams()\name$ = "blue"
AddElement(teams()\members()) : teams()\members() = "Cy"
ForEach teams()
line$ = teams()\name$ + ":"
ForEach teams()\members()
line$ + " " + teams()\members()
Next
PrintN(line$)
Next
FirstElement(teams())
Define copy.Team
copy = teams() ; its own list of members
LastElement(copy\members())
AddElement(copy\members()) : copy\members() = "Dee"
PrintN(Str(ListSize(copy\members())) + " " + Str(ListSize(teams()\members())))
Output:
red: Ann Bob
blue: Cy
3 2
Pointers
x has two kinds of pointer, one from each language.
PureBasic pointers: *name
The * is part of the variable's name. Arithmetic on these pointers counts in bytes.
Structure Point
x.i
y.i
EndStructure
OpenConsole()
Define p.Point
*q.Point = @p ; @ takes an address
*q\x = 7 ; fields through the pointer
*q\y = *q\x * 2
PrintN(Str(p\x) + "," + Str(p\y))
Output:
7,14
Built-in structures
As in PureBasic, each basic type also exists as a one-field structure, named after the type with its tag as the field:
| Structure | Field | Structure | Field | |
|---|---|---|---|---|
Byte |
b.b |
Long |
l.l |
|
Ascii |
a.a |
Integer |
i.i |
|
Character |
c.c |
Quad |
q.q |
|
Unicode |
u.u |
Float |
f.f |
|
Word |
w.w |
Double |
d.d |
|
String |
s.s |
They give memory a type: a *p.Long pointer reads and writes 4 bytes through *p\l. A tag written as a whole word (.Long) means the structure; the short tag .l means the plain type.
OpenConsole()
s$ = "Hi!"
*c.Character = @s$
While *c\c
n + 1
sum + *c\c
*c + SizeOf(Character)
Wend
PrintN(Str(n) + " chars, sum " + Str(sum))
*mem = AllocateMemory(8)
*v.Long = *mem
*v\l = 123456
PrintN(Str(PeekL(*mem)) + " " + Str(SizeOf(Long)))
Output:
3 chars, sum 210
123456 4
C pointers: Type*
A C pointer's type includes what it points to. Arithmetic counts in elements: adding 1 moves by the size of the pointed-to type.
Structure Point
x.i
y.i
EndStructure
OpenConsole()
Define p.Point
Point* r = &p
r->x = 5
r->y = 6
PrintN(Str(p\x + p\y))
Output:
11
Either kind can be assigned to the other without a cast.
*p.Point |
Point* p |
|
|---|---|---|
p + 1 |
next byte | next Point |
| fields | *p\x |
p->x, p\x, p.x |
| address of | @x |
&x |
Reading and writing memory
PeekL(address), PokeL(address, value) and their relatives read and write a value of a given type at any address. See the Memory library.
OpenConsole()
Dim values.l(3)
values(2) = 99
*p = @values(0)
PrintN(Str(PeekL(*p + 2 * 4))) ; byte arithmetic: element 2 is 8 bytes in
Output:
99
With ✅
Inside With expression … EndWith, a field written with a leading \ belongs to that expression: \x means expression\x. The expression is written out again for every \field, so it is worked out at each use, as in PureBasic: with With a(i), changing i inside the block moves to another element. With blocks nest, and the innermost one counts.
Structure Vec
x.i
y.i
EndStructure
Structure Ship
name$
pos.Vec
EndStructure
OpenConsole()
Dim fleet.Ship(1)
For i = 0 To 1
With fleet(i)
\name$ = "ship" + Str(i)
\pos\x = i * 100
With \pos
\y = \x + 5
EndWith
EndWith
Next
With fleet(1)
PrintN(\name$ + " " + Str(\pos\x) + "," + Str(\pos\y))
EndWith
Output:
ship1 100,105
Not yet available 🔜
- Unions (
StructureUnion) in generated code: they are laid out, but not tested.