ex.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 and VARSEG Functions 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 example, you must separately compile the C function
' with the large-model (/AL) switch and either place the object module in
' a Quick library or link it to the Basic program.
 
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
 
 DEFINT A-Z
 DECLARE SUB AddArr CDECL (BYVAL Offs, BYVAL Segm, BYVAL Num)
 DIM A(1 TO 100) AS INTEGER
 
 CLS                          ' Clear the screen
 FOR I = 1 TO 15              ' Fill the array with the numbers 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, place 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)
 FOR I = 1 TO 15              ' Print the modified array
     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]++;
' }