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

int8

unsigned char, byte

uint8

short

int16

unsigned short

uint16

int

int32

long (Windows®)

int32,
long

long (Linux®)

int64,
long

unsigned int

uint32

unsigned long (Windows)

uint32,
long

unsigned long (Linux)

uint64,
long

float

single

double

double

char *

1xn char array

*char[]

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 *

doublePtr

double

float *

singlePtr

single

integer pointer types (int *)

(u)int(size)Ptr

(u)int(size)

Matrix of signed bytes

int8Ptr

int8

Null-terminated string passed by value

cstring

1xn char array

Array of pointers to strings (or one **char)

stringPtrPtr

cell array of strings

enum

enumPtr

 

type **

Same as typePtr with an added Ptr (for example, double **
is doublePtrPtr)

lib.pointer object

void *

voidPtr

 

void **

voidPtrPtr

lib.pointer object

C-style structure

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')
get(sci)
    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?