C晶绑定:参数const的无符号字符**
问题描述:
这是我试图使用(其产生二进制数据的阵列)C函数的签名:C晶绑定:参数const的无符号字符**
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
答
这工作:
data = uninitialized UInt8*
MyLib.output(pointerof(data))
请注意,你有说法是UInt8**
,所以你需要声明UInt8*
类型的变量。
然而,水晶支持这个成语真的很好,与out
关键字:https://crystal-lang.org/docs/syntax_and_semantics/c_bindings/out.html
MyLib.output(out data)
# use data
这最后一种方法是首选,因为它更干,你不必重复类型。
还要小心,长通常映射到Int64。一般来说,在LibC
下有很好的别名,例如LibC::Char
,LibC::Long
等。
作品就像一个魅力:) - 谢谢你asterite – Mat