[racket] how to create a ctype array
_array produces a ctype, not an actual array itself. To get an array you can create one with malloc or you can call a C function through the ffi.
If you use malloc then you get back a generic #<cpointer> type which you can convert to an array using ptr-ref as per the docs:
"Since an array is treated like a struct, casting a pointer type to an array type does not work. Instead, use ptr-ref with a pointer, an array type constructed with _array, and index 0 to convert a pointer to a Racket representation that works with array-ref and array-set!."
-> (require ffi/unsafe)
-> (define t (_array _int 3))
-> (define x (malloc t))
-> (define a (ptr-ref x t 0))
-> a
#<array>
-> (array? a)
#t
-> (array-ref a 1)
1056768
-> (array-set! a 1 12)
-> (array-ref a 1)
12
Or as an ffi function type (-> _int -> (_array _int 3)), although I didn't test that.
On 12/13/2012 11:48 PM, ??? wrote:
> in the reference, I see the _array function, but I don't understand well, when i try to do, always output errors, can anyone give me an example?
> here is my test in REPL:
>
> racket@> (_array _int 3)
> #<ctype>
> racket@> (array? (_array _int 3)) ; why?
> #f
> racket@>
>
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121214/0bdbfc90/attachment.html>