首页 文章

LuaJIT ffi:如何将字符串数组传递给c函数

提问于
浏览
2

我想将存储字符串的Lua表传递给c函数 . 所以,例如,如果我有

tStr = {"String1", "String2", "String3"}

我如何传递给C函数 . 我想我必须打电话给ffi.new但是哪种类型我不确定..

local cVar = ffi.new("??" , tStr)  -- I am not sure what to pass as type

参数

另外在C函数中,我不确定如何访问整个数据,是否将字符串指针指向字符串,** str ??

void cFunction(**str); --What pointer type should be used here ??

...如果我错过了一些明显的问题,请道歉 . 但我刚开始使用Lua和ffi . 所以我仍然没有意识到大部分事情......

1 回答

  • 2

    这是一个简单的例子:

    local ffi = require"ffi"
    ffi.cdef"int execvp(const char*file, const char**argv);"
    local arg = ffi.new("const char*[3]", {"ls", "-l"})
    ffi.C.execvp(arg[0], arg)
    

    请注意常量3(数组的大小)
    等于2(从Lua传递的字符串数 {"ls", "-l"}
    加1(数组中的最后一个元素实际上是零终止符) .

相关问题