Skip to content
Apoorv Singal edited this page Aug 30, 2020 · 7 revisions

An array is a collection of data items, all of the same types, accessed using a common name.

Usage

import "mem.vo" // https://github.com/volantlang/volant/blob/main/lib/mem.vo

func main() i32 {
    array: [5]i32; // allocate 10*sizeof(i32) on stack, allocated memory is not initialized
    
    mem.set(cast(*void)array, 0, sizeof(array));  // initialize with mem.set

    array2: [5]i32 = {0, 1, 2, 3, 4}; // initialize with array initializers

    array3 := ([5]i32){4, 3, 2, 1, 0}; // allocate and initialize with compound literals

    return 0;
}

Elements of the array are accessed like array[index] where index is an expression of any numeric type.

Variables of any arbitrary number of dimensions and length are allowed in Volant, but variable-length arrays, i.e, arrays whose length cannot be determined during compile-time, are not allowed. However, you can use the heap to create arrays of variable length. Functions are also not allowed to return arrays.

Clone this wiki locally