Before you can access elements of an array, you need to set aside storage for that array. Fortunately, array declarations build on the declarations you've already seen. To allocate n
elements in an array, you would use a declaration like the following in one of the variable declaration sections:
ArrayName
:basetype
[n];
ArrayName
is the name of the array variable and basetype
is the type of an element of that array. This sets aside storage for the array. To obtain the base address of the array, just use ArrayName
.
The [n]
suffix tells HLA to duplicate the object n
times. Now let's look at some specific examples.
static CharArray: char[128]; // Character array with elements 0..127. ByteArray: byte[10]; // Array of bytes with elements 0..9. PtrArray: dword[4]; // Array of double words with elements 0..3.
These examples all allocate storage for uninitialized arrays. You may also specify that the elements of the arrays be initialized using declarations like the following in the static
and readonly
sections:
RealArray: real32[8] := [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]; IntegerAry: int32[8] := [ 1, 1, 1, 1, 1, 1, 1, 1 ];
These definitions both create arrays with eight elements. The first definition initializes each 4-byte real value to 1.0, the second declaration initializes each int32
element to 1. Note that the number of constants within the square brackets must exactly match the size of the array.
This initialization mechanism is fine if you want each element of the array to have the same value. What if you want to initialize each element of the array with a (possibly) different value? No sweat, just specify a different set of values in the list surrounded by the square brackets in the example above:
RealArray: real32[8] := [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; IntegerAry: int32[8] := [ 1, 2, 3, 4, 5, 6, 7, 8 ];