我如何在ffi中表示int *?

时间:2018-10-09 05:38:29

标签: c node.js pointers ffi

我在nodejs中使用FFI来连接C代码。 我对如何在界面中表示int *感到困惑。

#include "math.h"

int add(int x, int y)
{
  return x + y;
}

int minus(int x, int y)
{
  return x - y;
}

void multiply(int x, int y, int *result)
{
  *result = x * y;
}

var int = ref.types.int;
...
var math = ffi.Library(mathlibLoc, {
    "add": [int, [int, int]],
    "minus": [int, [int, int]],
    "multiply": [void, [int, int, ??]]
});

module.exports = math;

如何在参数列表中表示int *类型? 我浏览了ref文档以了解它,并找到了这样的示例代码。

var buf = new Buffer(4)
buf.writeInt32LE(12345, 0)

// first, what is the memory address of the buffer?
console.log(buf.address())  // ← 140362165284824

// using `ref`, you can set the "type", and gain magic abilities!
buf.type = ref.types.int

// now we can dereference to get the "meaningful" value
console.log(buf.deref())  // ← 12345


// you can also get references to the original buffer if you need it.
// this buffer could be thought of as an "int **"
var one = buf.ref()

但是不清楚如何表示指针类型?有人可以解释一下吗?

0 个答案:

没有答案