Arrays and Matrices in C Code
In C code, arrays/matrices are available for all basic data types, enumerations, and records. The variables are accessed through the array/matrix indices, the first index position of each dimension is 0.
An array/matrix can be added to a class or module by adding it to the Outline tab in the C code editor in the same way as described for block diagram components in Creating an Array or Matrix. The array/matrix elements are accessed through the array/matrix indices x and y, the first position for each index is 0.
The maximal array or matrix size can be edited in the Properties editor, the actual size can be edited in the table editor.
The array/matrix size cannot be modified at runtime. The maximum size for arrays is 65535 elements, the maximum size for matrices is 1048576 elements, with not more than 65535 elements per dimension.
Array/matrix data can either be edited in the table editor or filed in from a tab-delimited ASCII file; see Table Editor (Editor for Composite Elements).
In C code, elements of an array or matrix can be read and written to using the following syntax:
array: | val = myArray[index]; myArray[index] = val; |
matrix: | val = myMatrix[indX][indY]; myMatrix[indX][indY] = val; |
The first statement in each row reads the value of the array/matrix element at position index or indX/indY and assigns it to the variable val, which must be the same type as the array/matrix. Since array/matrix indices start from 0, myArray[3] returns the fourth element of myArray.
The second statement sets the value of the array/matrix element at position index or indX/indY to val, which must be the same data type as the array or matrix.
The actual size of an array/matrix can be determined using the following syntax:
array: | length = myArray.length(); |
matrix: | lengthX = myMatrix.xLength(); lengthY = myMatrix.yLength(); |
Arrays/matrices of record type do not support actual, or current size. For such elements, length or lengthX and lengthY return the max. size.
Arrays and matrices can have fixed or variant sizes. In addition, arrays/matrices of scalar or enumeration type and kind Variable can have variable sizes.
In the context of a project, you can use the Protected Vector Indices option to protect array/matrix indices against over- or underflow.
You can use the Generating Sparse Arrays target option to optimize the generated initialization code for arrays/matrices of scalar or enumeration type; see Initialization of Arrays and Matrices.
See also
Properties Editor for Basic Elements
Table Editor (Editor for Composite Elements)
Variant Size for Arrays and Matrices
Variable Size for Arrays and Matrices