next up previous contents index
Next: Dynamic Resizing Up: Arrays Previous: Declaring and Defining Arrays   Contents   Index

Initialization

We have seen that array elements can be initialized individually by assignment. It is also possible to initialize all or part of an array as a block, using the syntax below:

array[index[,...]] = [a, b, c, ... ]

The left side represents a starting address, in the format of an array element reference. The outer square brackets are explicit, the inner square brackets represent optional higher dimension indices and are not explicit. The square brackets on the right side are explicit, and entries are separated by commas and optional white space. One can use backslash-continuation to break a long initializer into multiple physical (but not logical) lines. The values from the right side are placed in the array starting at the indicated address, in the natural order of array scalar access. The array size is expanded when necessary. The line also serves to declare the array.

The a, b, c, ... can be expressions, or most commonly simple numbers.

Example:

ary[0] = [1, 2, 3, 4]

This declares and creates a size 4 array named ary, with components 1, 2, 3, 4. This is equivalent to the lines

ary[0] = 1
ary[1] = 2
ary[2] = 3
ary[3] = 4


next up previous contents index
Next: Dynamic Resizing Up: Arrays Previous: Declaring and Defining Arrays   Contents   Index
Stephen R. Whiteley 2017-04-09