Subject:
[ruby-ffi] Re: char array inside struct
From:
daniel åkerud <daniel.akerud@gmail.com>
Date:
11/2/09 3:39 AM
To:
ruby-ffi


On Oct 30, 3:20 pm, Wayne Meissner <wmeiss...@gmail.com> wrote:
> 2009/10/30 daniel åkerud <daniel.ake...@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

Yes that works well. Thanks,
/D