Main Content

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.

2. Open the Embedded Coder app.

3. In the C Code tab, select Code Interface > Individual Element Code Mappings.

4. In the Code Mappings editor, on the Inports tab, set Storage Class for the Inport block to ImportedExternPointer.

5. 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