Subject:
[ruby-ffi] Re: char array inside struct
From:
Wayne Meissner
Date:
10/30/09 9:20 AM
To:
ruby-ffi@googlegroups.com


2009/10/30 daniel åkerud <daniel.akerud@gmail.com>:
>
> I have a char array baked into a struct (not a pointer), and I just
> want a confirm that I do it the correct/intended way.
>
> struct {
>  uint8 Value;
>  uint8 String[SIZE_OF_ARRAY];
> } MyArray_t;
>
> class MyArray_t < FFI::Struct
>  layout  :Value,  :uint8,
>         :String, [:uint8, SIZE_OF_ARRAY]
> end

Yes, that is the correct way to declare array fields in structs.

>
> my_array_struct = MyArray_t.new
> c_function(my_array_struct.pointer) #the c_function fills the char
> array
> str = my_array_struct[:String].to_s # is to_s the correct function to
> retrieve a string? read_string doesn't work.

You should be able to get the string via:

my_array_struct[:String].to_ptr.read_string