Passing Arguments to Shared Library Functions

C and MATLAB Equivalent Types

The shared library interface supports all standard scalar C types. The following table shows these C types with their equivalent MATLAB® types. MATLAB uses the type from the right column for arguments having the C type shown in the left column.

    Note:   All scalar values returned by MATLAB are of type double.

MATLAB Primitive Types

C TypeEquivalent MATLAB Type

char, byte


unsigned char, byte




unsigned short




long (Windows®)


long (Linux®)


unsigned int


unsigned long (Windows)


unsigned long (Linux)






char *

char array (1xn)


cell array of strings

The following table shows how MATLAB maps C pointers (column 1) to the equivalent MATLAB function signature (column 2). In most cases, you can pass a variable from the Equivalent MATLAB Type column to functions with the corresponding Argument Data Type. See Pointer Arguments in C Functions for information about when you might choose to use a lib.pointer object instead.

MATLAB Extended Types

C Pointer TypeArgument Data TypeEquivalent MATLAB Type

double *



float *



intsize * (integer pointer types)

For example, int64 * becomes int64Ptr.





char[] (null-terminated string passed by value)


char array (1xn)

char ** (array of pointers to strings )


cell array of strings




type **

For example, double **
becomes doublePtrPtr.

lib.pointer object

void *



void **


lib.pointer object

struct (C-style structure)


MATLAB struct

mxArray *

MATLAB array

MATLAB array

mxArray **

MATLAB arrayPtr

lib.pointer object

Passing Arguments

Here are some important things to note about the input and output arguments shown in the Functions in library shrlibsample listing:

  • Many arguments (like int32 and double) are similar to their C counterparts. In these cases, you need only to pass in the MATLAB types shown for these arguments.

  • Some C arguments (for example, **double, or predefined structures), are different from standard MATLAB types. In these cases, you can either pass a standard MATLAB type and let MATLAB convert it for you, or you convert the data yourself using the MATLAB functions libstruct and libpointer. For more information, see Manually Converting Data Passed to Functions.

  • C functions often return data in input arguments passed by reference. MATLAB creates additional output arguments to return these values. Note that in the listing in the previous section, all input arguments ending in Ptr or PtrPtr are also listed as outputs.

Guidelines for Passing Arguments

  • Nonscalar arguments must be declared as passed by reference in the library functions.

  • If the library function uses single subscript indexing to reference a two-dimensional matrix, keep in mind that C programs process matrices row by row while MATLAB processes matrices by column. To get C behavior from the function, transpose the input matrix before calling the function, and then transpose the function output.

  • Use an empty array, [], to pass a NULL parameter to a library function that supports optional input arguments. This is valid only when the argument is declared as a Ptr or PtrPtr as shown by libfunctions or libfunctionsview.

Passing a NULL Pointer

You can create a NULL pointer to pass to library functions in the following ways:

  • Pass an empty array [] as the argument.

  • Use the libpointer function:

    p = libpointer; % no arguments 
    p = libpointer('string') % string argument
    p = libpointer('cstring') % pointer to a string argument
  • Use the libstruct function:

    p = libstruct('structtype'); % structure type  

Creating an Empty libstruct Object

To create an empty libstruct object, call libstruct with only the structtype argument. For example:

sci = libstruct('c_struct')
    p1: 0
    p2: 0
    p3: 0

MATLAB displays the initialized values.

Manually Converting Data Passed to Functions

Under most conditions, MATLAB software automatically converts data passed to and from external library functions to the type expected by the external function. However, you may choose to convert your argument data manually. Circumstances under which you might find this advantageous are:

  • When you pass the same piece of data to a series of library functions, you can convert it once manually before the call to the first function rather than having MATLAB convert it automatically on every call. This reduces the number of unnecessary copy and conversion operations.

  • When you pass large structures, you can save memory by creating MATLAB structures that match the shape of the C structures used in the external function instead of using generic MATLAB structures. The libstruct function creates a MATLAB structure modeled from a C structure taken from the library. See Working with Structure Arguments for more information.

  • When an argument to an external function uses more than one level of referencing (e.g., double **), you must pass a pointer created using the libpointer function rather than relying on MATLAB to convert the type automatically.

Was this topic helpful?