[racket] Filling char arrays inside cstructs

From: Diogo F. S. Ramos (diogofsr at gmail.com)
Date: Wed Feb 6 12:07:20 EST 2013

How do I fill a char array, inside a cstruct, with a byte string?

I'm using the following code to do it, but is there a better way?

#lang racket/base

(require ffi/unsafe)

;;; struct foo {
;;;   int a;
;;;   char b[42];
;;; };
;;;
;;; struct foo my_foo;
;;; my_foo.a = 7;
;;; strcpy(my_foo.b, "hello, world");

(define-cstruct _foo ((a _int)
                      (b (_array/list _byte 42))))

(define my-byte-string #"hello, world")
(define my-foo
  (make-foo 7
            (bytes->list
             (bytes-append my-byte-string
                           (make-bytes
                            (- 42 (bytes-length my-byte-string)))))))

Posted on the users mailing list.