HTBasic Help
×
Menu
Index

String Arrays

 
A string array generates two pointers. The first points to the string data area and the second points to the array dimension table. The string data area contains the string elements in row-major order.
 
A string element is made up of a short int current string length followed by the string data. Each string element is allocated with enough string data space to contain the maximum dimensioned string length rounded up to an even length.
 
This example demonstrates setting a string array element from a C routine.
 
BASIC Program
10 DIM S$(10)[40]
20 CALL Streint( S$(*), 4)
30 PRINT LEN(S$(4)),S$(4)
40 END
50 SUB Streint( S$(*), INTEGER E )
60   S$(E) = "This is a test of strings"
70 SUBEND
 
Prototype
1  END
10 SUB Streint( A$(*), INTEGER E )
20 SUBEND
 
C Program
#include "csub.h"
#include <string.h>
 
streint( npar, a, d, e )
int npar;             /* number of parameters */
strptr a;             /* string data pointer */
dimptr d;             /* dimension pointer */
intptr e;             /* element number to set */
{
strptr t;             /* string element pointer */
int maxsize = d->elen;/* get element size */
 
if( *e > d->cae )     /* check element number */
 return( 18 );       /* too large, return error */
 
if( maxsize < 25 )    /* check variable length */
 return( 18 );       /* too small, return error */
 
/* Notice the string element address calculation */
if( maxsize & 1)      /* if odd length */
 ++maxsize;          /* round up to even length */
t = (strptr)((U_CHAR *)a + ((maxsize + sizeof(T_SUBS)) * *e));
 
memcpy( t->str, "This is a test of strings", 25);
t->clen = 25;         /* set the length */
return( 0 );          /* no error */
}
 
Note: Don’t use the strcpy( ) function because it will append an un-wanted NULL.