Pointers

Pointers are a core feature of the C programming language. YAKINDU Statechart Tools' Deep C/C++ Integration is making C pointers available to you in your statecharts. In particular, you can

  • declare typed pointer variables,
  • assign a pointer to a pointer variable of the same type,
  • retrieve the pointer pointing to a given variable,
  • pass pointers as parameters to functions, and
  • receive a pointer as a functions return value.

Declaring pointer variables

Pointer variables are declared in a statechart’s definition section as shown in the following example:

var n: int32_t
var pInt: pointer<int32_t>
var ppInt: pointer<pointer<int32_t> >
var pTriangle: pointer<Triangle>;

The declarations above declare

  • n as a (non-pointer) variable of type int32_t,
  • pInt as a pointer variable that is pointing to a variable of type int32_t,
  • ppInt as a pointer that is pointing to a pointer that is pointing to a variable of type int32_t, and
  • pTriangle as a pointer to a variable of the self-defined type Triangle.

Please note: When closing the type specification in a pointer declaration with angle brackets, e.g., pointer<pointer<int32_t> >, the > characters must be separated from each other by one or more white space characters. Writing, for example, pointer<pointer<int32_t>> would result in an error. This restriction will be fixed in a later release.

Using pointer variables

In order to actually assign a pointer to a pointer variable, you have to get hold of that pointer. To retrieve the pointer to a variable v, use v's extension function pointer. That is, for a variable v, the expression v.pointer evaluates to a pointer to v. Each variable has the pointer extension function.

Example: Let’s say the pointer variable pInt (declared in the example above) should point to the variable n. The following assignment accomplishes this:

pInt = n.pointer

Similarly, a pointer to a pointer to a base type can be retrieved as follows:

ppInt = pInt.pointer;

Or even:

ppInt = n.pointer.pointer

In order to deference a pointer, i. e. to retrieve the value of what the pointer is pointing to, use the value extension function, which is available on all pointer-type variables.

Example: Let’s say the pointer variable pInt (declared in the example above) is pointing to some int32_t variable. The value of the variable pInt is pointing to should be assigned to the int32_t variable n. The following assignment accomplishes this:

n = pInt.value;

Similarly, if ppInt points to a pointer pointing to some int32_t variable, the following statement retrieves the latter’s value:

n = ppInt.value.value;

Passing pointer parameters to C functions is straightforward. Let’s say you have a C function to rotate a triangle around a center point by a given angle. The C function is defined like this:

Triangle* rotateTriangle(Triangle* triangle, Point* centerPoint, float angle) { … }

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

pTriangle2 = rotateTriangle(pTriangle, pCenterPoint, 45.0);

Please note: Assigning a pointer to a pointer variable is only possible if the pointer types are the same.