crubycallbackruby-ffi

Declaring char array in Ruby FFI


I have the following C code:

typedef void (*mycallback) (char buf[128]);
void take_callback(mycallback cb)
{
}

I've written the equivalent Ruby FFI declarations as below (following advice for structs on FFI wiki):

  callback :mycallback, [[:char, 128]], :void
  attach_function :take_callback, [:mycallback], :void

When I run it, I get the following error:

`find_type': unable to resolve type '[:char, 128]' (TypeError)

It seems I'm not declaring the char array in the callback correctly. From the way arrays work in function arguments in C, I think I should use :pointer instead of [:char, 128] . But I'm not sure about the peculiarities of FFI. What's really the correct syntax here?


Solution

  • Arrays aren't passed by value in C -- they're passed as pointers to the first element, so :pointer (or whatever is ordinarily used for char *) should be correct.