bas7ex.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
VARPTR, VARSEG Functions Programming Example
                       Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
'This example uses the VARPTR and VARSEG functions in a CALL statement
'to pass a BASIC array to a C function.
 
'Note: To run this program, you must separately compiled the C function
'with the large-model (/AL) switch and place the object module in a
'Quick library or link it to the BASIC program.
 
DEFINT A-Z
DECLARE SUB AddArr CDECL (BYVAL Offs, BYVAL Segm, BYVAL Num)
DIM A(1 TO 100) AS INTEGER
'Fill the array with the numbers 1 to 15.
FOR I = 1 TO 15
   A(I) = I
NEXT I
 
'Call the C function. AddArr expects a far address (segment and offset).
'Because CDECL puts things on the stack from right to left, put the offset,
'VARPTR(A(1)), first in the list, followed by the segment, VARSEG(A(1)).
 
CALL AddArr(VARPTR(A(1)), VARSEG(A(1)), 15)
 
'Print the modified array.
FOR I = 1 TO 15
   PRINT A(I)
NEXT I
END
 
 
'/* C Function AddArr */
'/* Add one to the first num elements of array arr.*/
'void far addarr(arr,num)
'int far *arr;
'int num;
'{
'    int i;
'    for(i=0;i<num;i++) arr[i]++;
'}