# How to initialize a cstruct that has an array?

**URL:** https://racket.discourse.group/t/how-to-initialize-a-cstruct-that-has-an-array/2414
**Category:** Questions & Answers
**Created:** [October 23, 2023, 2:19pm UTC](https://racket.discourse.group/t/how-to-initialize-a-cstruct-that-has-an-array/2414 "2023-10-23T14:19:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![db7](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/db7/32/1470_2.png) [@db7](https://racket.discourse.group/u/db7)
#### Post date: [October 23, 2023, 2:19pm UTC](https://racket.discourse.group/t/how-to-initialize-a-cstruct-that-has-an-array/2414/1 "2023-10-23T14:19:06Z")

</div>

Hi all,

I'd like to create a cstruct which has an array as one of its fields. For the sake of the example, let's say the array is an array of integers. When I try create a new instance of the cstruct, I don't know how to initialize the array field. Here is a simple example:

```scheme
#lang racket

(require ffi/unsafe)
(define _args (make-array-type _uint64 4))
(define-cstruct _mystruct (
                         [id _uint8]
                         [flag _bool]
                         [args _args]))
 
(make-mystruct 123 #f #(1 2 3 4))

```

The error I get is this:

```scheme
$ racket array.rkt
ptr-set!: given value does not fit primitive C type
  C type: (_array ....)
  given value: '#(1 2 3 4)
  context...:

```

It must be something simple that I am missing. Can somebody give me a hint?

Thx, -Diogo

---

<div class="post-metadata">

### Author: ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)
#### Post date: [October 24, 2023, 5:24am UTC](https://racket.discourse.group/t/how-to-initialize-a-cstruct-that-has-an-array/2414/2 "2023-10-24T05:24:48Z")

</div>

You can use [`_array/vector`](https://docs.racket-lang.org/foreign/C_Array_Types.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29.__array%2Fvector%29%29):

```scheme
#lang racket/base

(require ffi/unsafe)
(define-cstruct _mystruct (
                         [id _uint8]
                         [flag _bool]
                         [args (_array/vector _uint64 4)]))
 
(define s (make-mystruct 123 #f #(1 2 3 4)))
(println (vector-ref (mystruct-args s) 1))

```

There's also [homogeneous numeric vectors](https://docs.racket-lang.org/foreign/homogeneous-vectors.html), but I've never been able to get their type macros like [`_u64vector`](https://docs.racket-lang.org/foreign/homogeneous-vectors.html#%28form._%28%28lib._ffi%2Fvector..rkt%29.__u64vector%29%29) to work with the rest of the FFI.

---

<div class="post-metadata">

### Author: ![db7](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/db7/32/1470_2.png) [@db7](https://racket.discourse.group/u/db7)
#### Post date: [October 24, 2023, 7:13am UTC](https://racket.discourse.group/t/how-to-initialize-a-cstruct-that-has-an-array/2414/3 "2023-10-24T07:13:12Z")

</div>

Thank you! Works as expected.
