2

这是我正在尝试使用的 C 函数的签名(它生成二进制数据数组):

long get_output( const unsigned char ** );

我将其映射为:

fun output = get_output( UInt8** ): Int32

在 C 中使用它的工作示例:

const unsigned char * data;
get_output( &data );

但在水晶中:

data = uninitialized UInt8
MyLib.output( pointerof( pointerof( data ) ) ) # ERR: pointerof of pointerof not allowed
4

1 回答 1

3

这有效:

data = uninitialized UInt8*
MyLib.output(pointerof(data))

请注意,您拥有的参数是UInt8**,因此您需要声明一个类型为 的变量UInt8*

但是,Crystal 非常好地支持这个成语,使用out关键字:https ://crystal-lang.org/docs/syntax_and_semantics/c_bindings/out.html

MyLib.output(out data)
# use data

最后一种方式是首选,因为它更干燥,您不必重复类型。

还要小心,long 通常映射到 Int64。通常在 下有很好的别名LibC,例如LibC::Char,LibC::Long等。

于 2016-12-17T15:37:19.700 回答