【问题标题】:Crystal C bindings: argument const unsigned char **Crystal C 绑定:参数 const unsigned char **
【发布时间】:2016-12-17 11:16:47
【问题描述】:

这是我尝试使用的 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

【问题讨论】:

    标签: crystal-lang


    【解决方案1】:

    这行得通:

    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::CharLibC::Long等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多