# How does \_array/list work?

**URL:** <https://racket.discourse.group/t/how-does-array-list-work/4078>\
**Category:** Questions & Answers\
**Tags:** question, ffi\
**Created:** [January 12, 2026, 5:26pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078 "2026-01-12T17:26:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MickeyKnox](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@MickeyKnox](https://racket.discourse.group/u/MickeyKnox)\
**Post date:** [January 12, 2026, 5:26pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/1 "2026-01-12T17:26:13Z")

</div>

I have a list that I want to convert into a C array. `_array/list` looks promising, as "the Racket representation is a list". But how exactly can I convert my list into a C array?

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [January 12, 2026, 8:58pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/2 "2026-01-12T20:58:08Z")

</div>

> **[Tutorial: Using Racket's FFI](https://prl.khoury.northeastern.edu/blog/2016/06/27/tutorial-using-racket-s-ffi/)**
>
> Update: this post is now part of a series. Part 2 is here and part 3 is here. I've seen several people ask for a tutorial on Racket's foreign function interface (FFI), which allows you to dynamically load C libraries for use in Racket code. While I...

I believe part 2 has an example.

---

<div class="post-metadata">

**Author:** ![MickeyKnox](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@MickeyKnox](https://racket.discourse.group/u/MickeyKnox)\
**Post date:** [January 13, 2026, 8:52pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/3 "2026-01-13T20:52:17Z")

</div>

Thank you, that was a very interesting read. Unfortunately, I couldn't apply the example in the blog. This is the function I want to call (from the raylib wrapper):

```scheme
(DrawLineStrip points pointCount color) → _void
  points : (_pointer-to _Vector2)
  pointCount : _int
  color : _Color

```

Now in order to create the points parameter I need to make use of some \_array function, correct? I've played a little with it and it doesn't really make sense:

```scheme
> (define bar (_array _int 5))
> (array? bar)
#f
> (array-set! bar 0 7)
; array-length: contract violation
; expected: array?
; given: #<compound-ctype>
; [,bt for context]
> (array-type bar)
; array-type: contract violation
; expected: array?
; given: #<compound-ctype>
; [,bt for context]
> 

```

Why does this not work? The documentation for \_array states:

> Creates an array type whose Racket representation is an array that works with [array-ref](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29._array-ref%29%29) and [array-set!](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29._array-set%21%29%29).

That doesn't seem to be correct. What am I missing?

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [January 13, 2026, 10:26pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/4 "2026-01-13T22:26:06Z")

</div>

From the documentation of

> **[Raylib](https://docs.racket-lang.org/raylib/index.html)**

I see that `Vector2` is a struct consisting of two floats.

> > [\_Vector2](https://docs.racket-lang.org/raylib/Generated_Raylib_Bindings.html#%28def._%28%28lib._raylib%2Fgenerated%2Fstructs..rkt%29.__.Vector2%29%29) : [ctype?](https://docs.racket-lang.org/foreign/ctype.html#%28def._%28%28quote._~23~25foreign%29._ctype~3f%29%29)  
> > struct
> > 
> > ([struct](https://docs.racket-lang.org/reference/define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) [Vector2](https://docs.racket-lang.org/raylib/Generated_Raylib_Bindings.html#%28def._%28%28lib._raylib%2Fgenerated%2Fstructs..rkt%29._.Vector2%29%29) (x y)  
> > #:constructor-name [make-Vector2](https://docs.racket-lang.org/raylib/Generated_Raylib_Bindings.html#%28def._%28%28lib._raylib%2Fgenerated%2Fstructs..rkt%29._make-.Vector2%29%29))  
> > x : [\_float](https://docs.racket-lang.org/foreign/Numeric_Types.html#%28def._%28%28quote._~23~25foreign%29.__float%29%29)  
> > y : [\_float](https://docs.racket-lang.org/foreign/Numeric_Types.html#%28def._%28%28quote._~23~25foreign%29.__float%29%29)

So I think, you can pass `(vector (make-Vector2 1. 2.) (make-Vector2 3. 4.))` for the first argument `points`.

---

<div class="post-metadata">

**Author:** ![MickeyKnox](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@MickeyKnox](https://racket.discourse.group/u/MickeyKnox)\
**Post date:** [January 13, 2026, 11:03pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/5 "2026-01-13T23:03:07Z")

</div>

Unfortunately, that doesn't work:

```scheme
cpointer-accessor: contract violation
  expected: cpointer?
  given: '#(#<cpointer> ...

```

In the meantime I've learned about cvector, which didn't work either:

```scheme
cpointer-accessor: contract violation
  expected: cpointer?
  given: #<cvector>

```

Edit: I found the `cvector-ptr` procedure, which did the trick 🙂

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [January 14, 2026, 5:31am UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/6 "2026-01-14T05:31:17Z")

</div>

`_array` creates a `ctype?` not an array. Either the array is allocated by the C library, or you can allocate it with `malloc` and then follow this guidance from the `make-array-type` docs:

> Since an array is treated like a struct, [cast](https://docs.racket-lang.org/foreign/Miscellaneous_Support.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29._cast%29%29)ing a pointer type to an array type does not work. Instead, use [ptr-ref](https://docs.racket-lang.org/foreign/foreign_pointer-funcs.html#%28def._%28%28quote._~23~25foreign%29._ptr-ref%29%29) with a pointer, an array type constructed with [\_array](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29.__array%29%29), and index 0 to convert a pointer to a Racket representation that works with [array-ref](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29._array-ref%29%29) and [array-set!](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29._array-set%21%29%29).

---

<div class="post-metadata">

**Author:** ![jjsimpso](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jjsimpso/32/602_2.png) [@jjsimpso](https://racket.discourse.group/u/jjsimpso)\
**Post date:** [January 14, 2026, 4:45pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/7 "2026-01-14T16:45:02Z")

</div>

Something like this should work:

```scheme
(define _shape-array-1d (_array _int 1))
(define shape (malloc _shape-array-1d))
(array-set! (ptr-ref shape _shape-array-1d 0) 0 10)
(array-ref (ptr-ref shape _shape-array-1d 0) 0)

```

This is from some example code I had commented out because dealing with array types in the FFI is a pain! There are ways to avoid having to do this manually, like letting the `fun` auto-convert from vectors for you or just using pointers.

---

<div class="post-metadata">

**Author:** ![MickeyKnox](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@MickeyKnox](https://racket.discourse.group/u/MickeyKnox)\
**Post date:** [January 15, 2026, 10:40pm UTC](https://racket.discourse.group/t/how-does-array-list-work/4078/8 "2026-01-15T22:40:11Z")

</div>

I think I get the idea of how arrays work, but I haven't implemented it. Instead I went for `cvector`. This is roughly what I'm doing:

```scheme
(define (draw-outline list-of-Vector2 color)
  (let ((cvec (list->cvector list-of-Vector2 _Vector2)))
    (DrawLineStrip (cvector-ptr cvec) (cvector-length cvec) color)))

```

soegaard said I can use a `vector`, but I couldn't make that work.

One question remains, just out of curiosity: Is `cvector` implemented with arrays under the hood? Or are a they implemented independently?
