Skip to content

Pointers

Boa supports pointers, with some niceties (and some limitations as well) that languages such as C don't have. The pointer type in Boa is noted *T, where T is the pointed type.

Simple example

  • Use the prefix & operator to take the address of a variable;
  • Use the postfix .* operator to derefence a pointer.
boa
let i = 12
let pi = &i // pi's type is *i32
pi.* = 15 // automatic dereferencement! 'i' has been updated

Automatic derefencement of pointers to structures

Unlike C, Boa does not have the -> operator because pointers to structures are automatically derefenced when trying to access one of its fields.

Here is an example in C++ and its equivalent in Boa:

cpp
struct Point {
    int x, y;
};

Point p { 12, 34 };
Point* pp = &p;

pp->x = 1212; // or (*pp).x = 1212;
boa
struct Point:
    x: i32
    y: i32

let p = Point(12, 34)
let pp = &p

pp.x = 1212 // automatic dereferencement using the . operator (no -> operator needed)

Limitations

No initialization from arbitrary values

Boa pointers must be initialized with the address of a variable; they cannot be directly initialized with a "raw" number:

boa
let i = 1234
let invalid_pi: *i32 = 0xDEADBEEF // invalid!
let valid_pi = &i // valid

No null pointers

In Boa, pointers must point to a valid address, so a *T cannot be null. Use an optional pointer ?*T (see the section about optionals for more information) to simulate a null pointer.

Required initialization on pointer declaration

Since Boa pointers cannot be null, they must be directly initialized when they are declared. It is not possible to declare a pointer and initialize it later, so the following code is invalid:

boa
let pp: *Point
// ...
pp = &p

No built-in pointer arithmetic

There is currently no pointer arithmetic built-in in the language.