# Ruby FFI: fixed size char array inside struct

**URL:** <https://www.ruby-forum.com/t/ruby-ffi-fixed-size-char-array-inside-struct/178030>\
**Category:** Ruby\
**Created:** [October 28, 2009, 8:56am UTC](https://www.ruby-forum.com/t/ruby-ffi-fixed-size-char-array-inside-struct/178030 "2009-10-28T08:56:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_SSSSkerud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@daniel\_SSSSkerud](https://www.ruby-forum.com/u/daniel_SSSSkerud)\
**Post date:** [October 28, 2009, 8:56am UTC](https://www.ruby-forum.com/t/ruby-ffi-fixed-size-char-array-inside-struct/178030/1 "2009-10-28T08:56:44Z")

</div>

(I dind’t find an official Ruby FFI mailing-list so I try here)

I have a char array baked into a struct (not a pointer), and I have  
problems describing it in a layout.

struct {  
uint8 Value;  
uint8 String[SIZE\_OF\_ARRAY];  
} MyArray\_t;

The only thing I’ve come up with is to have a dummy variable at the end.

class MyArray\_t \< FFI::Struct  
layout :Value, :uint8,  
:String, :char\_array,  
:Dummy, :uint8, SIZE\_OF\_ARRAY+1 # TODO TODO  
end

And I use it something like this:

arr = MyArray\_t.new  
c\_function(arr.pointer) # calls a c function that fills the string  
str\_ptr = arr.pointer + arr.offset\_of(:String)  
puts str\_ptr.read\_string()

I need the dummy otherwise there wont be enough bytes allocated and  
c\_function overwrites the heap.  
Any and all help appreciated. (Note that it’s a legacy API,  
redesigning the C-interface is not possible.)

/D

---

<div class="post-metadata">

**Author:** ![daniel\_SSSSkerud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@daniel\_SSSSkerud](https://www.ruby-forum.com/u/daniel_SSSSkerud)\
**Post date:** [October 28, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/ruby-ffi-fixed-size-char-array-inside-struct/178030/2 "2009-10-28T14:32:10Z")

</div>

On Oct 28, 4:55 am, daniel åkerud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (I dind’t find an official Ruby FFI mailing-list so I try here)

[http://groups.google.com/group/ruby-ffi](http://groups.google.com/group/ruby-ffi)
