[racket] how can I convert a list or vector to ctype array directly?

From: 刘海宽 (h.kuan.liu at gmail.com)
Date: Sat Dec 15 02:33:20 EST 2012

Instead through iteration like for or do which assign values to element of array, deliver list or vector directly to the ctype function imported by ffi.
Because I think that operating the list or vector with built-in functions is more conveniently than the ctype array.
In the reference, I find _array/list and _array/vector function, here is the description:

"Like _array, but the Racket representation is a list (or list of lists for a multidimensional array) of elements copied to and from an underlying C array."

and I test the two functions, after deliver their cpointers as arguments to ctype function, the list or vector doesn't change. here is my code:

#lang racket
(require ffi/unsafe
         ffi/unsafe/define)

(define-ffi-definer define-sm (ffi-lib "scalam"))

;;----------------------------------------------
 ; sm add element of array with 1.
;
; int sm(int m, int n, double * p)
; {
;     int i, j;
;     double *p_end;
;     p_end = p + (m * n) - 1;
;    for (;p <= p_end; p++)
;         (*p) = (* p) + 1;
;    
;     return 0;
;  }
;---------------------------------------------------------
(define-sm sm (_fun _int _int  _pointer -> _int)) 

(define t (_array/vector _double 3))
(define x (malloc t))
(define a (ptr-ref x t 0))

(for ((i '(0 1 2)))
    (vector-set! a i (+ i 1.0)))

(define info (sm 3 1 x))

(for ((i '(0 1 2)))
    (display (vector-ref a i))

;=====================
a doesn't change. or is there something wrong?

thank you.
------------------
lhk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121215/537782d0/attachment.html>

Posted on the users mailing list.