Skip to content

Arrays

Unlike other variables, arrays are not defined in a statechart’s definition section, but rather on the C side in header files. Importing a C header containing an array definition makes the array available to a statechart.

While itemis CREATE’s Deep C/C++ Integration provides mechanisms for accessing individual elements of an existent array, arrays must be allocated statically or dynamically in C. Initializing the array elements is possible in C as well as in the statechart. However, depending on the concrete application it might generally be easier in C.

From version 5.1 it is also possible to use the array access on pointers that are declared in the statechart itself.

The header file sample_arrays.h defines a couple of sample arrays:

#ifndef SAMPLE_ARRAYS_H_
#define SAMPLE_ARRAYS_H_

#include <stdint.h>
#include "triangle.h"

int32_t coordinates[] = {0, 0, 10, 0, 5, 5};

Triangle manyTriangles[200];

int32_t * pArray[10];

#endif /* SAMPLE_ARRAYS_H_ */

The following arrays are defined:

  • coordinates is statically allocated to hold six int32_t elements and it is initialized with values for all six of them. More precisely, the number of elements in the initializer determines the size of the array.
  • manyTriangles is statically allocated with enough memory to hold 200 elements of the self-defined Triangle type. However, these elements are not initialized. This can and should be done either in C or in the state machine. An example is given below.
  • pArray is of size 10 and holds pointers to int32_t values.

As mentioned above, importing a header file containing array definitions into the statechart’s definition section is sufficient to make the arrays available in a statechart. Example:

import: sample_arrays

With this import, you can access the arrays in statechart language expressions, for example in a state’s local reactions:

entry /
coordinates[2] = 42

Writing to array elements is as straightforward as you would expect. Examples:

coordinates[0] = coordinates[0] + 1;
pArray[3] = n.pointer;
pArray[4] = coordinates[0].pointer

Passing arrays as parameters to C functions is straightforward. Let’s say you have a C function sort to sort the elements of a one-dimensional int32_t array and return a pointer to the sorted array:

int32_t* sort(int32_t data[], int size) {…}

Please note that in C a function cannot return an array as such, but only a pointer to it. Analogously you cannot pass an array by value as a parameter to a function, i. e. the data bytes the array is consisting of are not copied into the function’s formal parameter. Instead a pointer to the array is passed to the function, or – to be more exact – a pointer to the array’s first element. To express this in the function’s formal parameter type, you can specify the sort function equivalently to the above definition as follows. The data parameter is now specified as int32_t* data instead of int32_t data[], but the meaning is exactly the same.

int32_t* sort(int32_t* data, int size) {…}

Provided the function is declared in an imported C header file, you can call it directly like this:

sort(coordinates, 6)

Please note: The current itemis CREATE release only supports statically allocated arrays. Arrays dynamically allocated using malloc() or calloc() will be supported in a later version.