Pointers

When your handwritten code allocates memory for signal, state, or parameter data, you can generate code that accesses that data through a pointer. Apply a storage class such as ImportedExternPointer to a data item in the model. Your handwritten code provides the pointer definition.

C Construct

extern double *myIn;

Procedure

1. Open example model ex_pointer. The model opens in the Simulink Editor Code perspective.

2. In the Model Data Editor, select the Inports/Outports tab.

3. From the Change View menu, select Code.

For the Inport block, Signal Name is In1 and Storage Class is ImportedExternPointer.

4. Generate code from the model.

Results

The generated header file ex_pointer.h declares the pointer.

/* Imported (extern) pointer block signals */
extern real_T *In1;                    /* '<Root>/In1' */

In the generated source file ex_pointer.c, in the model step function, the algorithm dereferences the pointer, In1.

/* Model step function */
void ex_pointer_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/In1'
   */
  rtY.Out1 = *In1;
}

Related Topics