Strings

Each language implements strings differently. This section describes the ways that strings are implemented in Microsoft languages.

C and C++ String Format

C and C++ store strings as arrays of bytes and use a null character ( '\0' ) as an end-of-string delimiter. For example, consider the following string:

char c_string[] = “C text string”;

This string is represented in memory as follows:

Because c_string is an array like any other, C and C++ pass it by reference in function calls.

Note that this does not apply to string classes written in C++.

BASIC String Format

BASIC stores strings as four-byte descriptors pointing to the actual string data. The format of the descriptor is as follows:

The first field of the string descriptor contains an integer indicating the length (in bytes) of the string. The second field contains the address of the string in the default data segment.

Do not attempt to alter the length of BASIC strings, because they are managed by BASIC string-space management routines. You cannot count on a particular string remaining at a given offset during the execution of a BASIC program because the BASIC string-space management routines allocate strings to different areas of memory depending on program requirements.

The format of the string at DS:Address is a simple array of characters. The string is exactly the length indicated in the descriptor.

Summary: To pass a BASIC string to C, append a null character.

Because C needs the null character to delimit the end of the string, you should append chr$( 0 ) to your BASIC string before passing it to your C function. For example,

A$ = “I am a BASIC string”

A$ = A$ + chr$( 0 )

CALL CFunc( SADD(A$) )

Note that the BASIC call is made by near reference using the SADD keyword.

Summary: Use a string descriptor to pass a C string to BASIC.

To pass a C string to BASIC, create a structure for the string descriptor. For example,

char c_string[] = “C String Data”;

struct tagBASICStringDes

{

char * sd_addr;

int sd_len;

} str_des;

str_des.sd_addr = c_string;

str_des.sd_len = strlen( c_string );

BASICFunction( &str_des );

FORTRAN String Format

FORTRAN stores strings as a series of bytes at a fixed location in memory. There is no delimiter at the end of the string. Consider the string declared as follows:

STR = 'FORTRAN STRING'

The string is stored in memory as follows:

FORTRAN passes strings by reference, as it does all other data.

Note:

FORTRAN's variable length strings cannot be used in mixed-language programming because the temporary variable used to communicate string length is not accessible to other languages.

To pass a C string to FORTRAN (or Pascal), pass the variable by reference as you normally would. In your FORTRAN or Pascal routine, you must specify the length of the string; strings that are passed as arguments from one language to another must be of fixed length.

Pascal String Format

Pascal represents strings as fixed-length arrays of CHAR or as strings with a length byte followed by the string data.

Summary: To pass a fixed-length string to C, append a null character.

To pass a fixed-length string to a C function, use the concatenation operator (*) to append a null character. Then pass the string to the C function by reference (by declaring the string as CONST, CONSTS, VAR, or VARS). For example,

PROGRAM PasStr( input, output );

type

stype15 = string(15); { fixed-length }

var

str : stype15;

PROCEDURE PasStrToC( VAR s1 : stype15 ) [C]; EXTERN;

BEGIN

str := 'Pass this to C' * chr( 0 );

PasStrToC( str );

END.

A more flexible way to pass Pascal strings to C functions is to declare them as type ADRMEM or ADSMEM, then pass the address of the string. For example,

PROCEDURE PasStrToC( s1adr : ADRMEM ) [C]; EXTERN;

Then you can call the C function with this code:

PasStrToC( ADR str );

Using this method, you can pass strings of different lengths to C functions.

Note:

The Pascal type LSTRING is not compatible with C; you can pass a string declared as LSTRING by first assigning it to another variable of type STRING, then passing that variable.

Whenever you pass a variable of type STRING or type LSTRING by value, Pascal pushes the whole string onto the stack and passes the length of the string as another parameter. C cannot access strings passed in this manner.

Passing a string from a C function to a Pascal function or procedure is identical to passing a string from a C function to a FORTRAN routine. The only provision you must make is to specify the length of the string to your Pascal function.