1

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?

G S
  • 35,511
  • 22
  • 84
  • 118

1 Answers1

2

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.

  • I just found this wiki page: https://github.com/ffi/ffi/wiki/Callbacks which confirms that I should use :pointer. – G S Apr 03 '12 at 01:03