Skip to content

Pointers

Apoorv Singal edited this page Aug 30, 2020 · 2 revisions

Pointers are variables that store the memory address of a value.

Type signature

* type is the type for a pointer that stores the memory address of a value of type type.

Accessing the memory address of a value

The memory address of value is accessed using the & operator. For example,

var: i32 = 0;
ptr := &var; // ptr holds the memory address of var

Accessing the value at a memory address

The value at a memory address is accessed using the * operator. For example,

var := 0; // create a i32 of value 0
ptr := &var; // create a pointer to var
var2 := *ptr; // create var2 of value ptr points to

Pointer math

Pointers are numbers, so all number operations are valid on pointers. For example, *(ptr + x) gives the value stored at ptr+x memory address.


See heap for more on pointers.

Clone this wiki locally