Array and Struct Literals

The value of an array is made up of the value of the individual array elements and the value of a struct is made up of the individual fields. A two-dimensional array is an array of arrays, a three-dimensional array is an array of arrays of arrays, etc. The elements of an array can be a struct and a field in a struct can be an array.

An array or struct literal is called a complex literal and consists of an open (left) brace, a list of element or field values separated by commas and a closing (right) brace. For example, the following is an assignment statement to set the value of an array of 3 Floats:

afX = {1.2, 3.45, 6.789}

and the following sets the value of a struct with four fields (Char, Double, String, Long);

usY = {'A', 3.14159, "xyz", 98765}

If the components of a complex literal are in turn complex literals, for example, an array of arrays or a struct with a field which is an array, the constants for each component follow the same syntax (open brace, list of values, close brace). This format can be nested as many levels as needed to match the data type definition. For example a 2 by 3 Short array could be set as follows:

an23 = { {101, 102, 103}, {201, 202, 203} }

and a struct with three fields (Char, array of 3 Shorts, Double) could be set as follows:

us2 = {'Z', {1,2,3}, 4.5678}

It is not necessary that the apparent type of the literals match the elements of the complex data type exactly. As long as the literal is assignment compatible with the type of the corresponding array element or struct field, the literal will be converted to the required type. For example, the following two arrays of 3 Floats each end up with exactly the same values:

af1 = {1,2,3}

af2 = {1.0, 2.0, 3.0}

See Also

Array Data Type, Arrays of Structs