When you use an array in a program written in a single language, the method for array handling is consistent. When you mix languages, you need to be aware of the differences between array-handling techniques in various languages.
Unlike most Microsoft languages, BASIC keeps an array descriptor, which is similar to the BASIC string descriptor discussed in “Strings”. This array descriptor is necessary because BASIC handles memory allocation for arrays dynamically (at run time). Dynamic allocation requires BASIC to shift arrays in memory.
Summary: To pass a BASIC array to a C function, use the VARPTR and VARSEG keywords.
The VARPTR and VARSEG keywords obtain the address of the first element of the array and its segment, respectively. The following example shows how to call a C function with a near reference and a far reference to an array:
DIM ARRAY%( 20 )
DECLARE CNearArray CDECL( BYVAL Addr AS INTEGER )
DECLARE CFarArray CDECL( BYVAL Addr AS INTEGER, BYVAL Seg AS INTEGER )
.
.
.
CALL CNearArray( VARPTR( ARRAY%(0) ) )
CALL CFarArray( VARPTR( ARRAY%(0) ), VARSEG( ARRAY%(0) ) )
The C functions receiving ARRAY can be declared as follows:
__cdecl CNearArray( int * array );
__cdecl CFarArray( int far * array );
The routine that receives the array must not make a call back to BASIC. If it does, the location of the array data could change, and the address that was passed to the routine would become meaningless.
If you only need to pass one member of the array from BASIC to your C function, you can pass it by value as follows:
CALL CFunc( ARRAY%(8) )