简体   繁体   English

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

[英]LuaJIT ffi : How to pass array of string to c function

I want to pass Lua table storing strings to c function. 我想将存储字符串的Lua表传递给c函数。 So for example if I have 所以,例如,如果我有

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

How do I pass to C function. 我如何传递给C函数。 I think I have to call ffi.new but which what type I am not sure.. 我想我必须打电话给ffi.new但是哪种类型我不确定..

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

parameter 参数

Also in C Function, I am not sure how to access the whole data, will it be string pointer pointing to string , **str ?? 另外在C函数中,我不确定如何访问整个数据,是否将字符串指针指向字符串,** str ??

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

... Apologies if I missed something obvious question. ...如果我错过了一些明显的问题,请道歉。 But I am just starting with Lua & ffi. 但我刚开始使用Lua和ffi。 so I am still not aware of most of the things .. 所以我仍然不知道大多数事情..

This is a simple example: 这是一个简单的例子:

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)

Please note that constant 3 (size of the array) 请注意常量3(数组的大小)
equals to 2 (the number of strings passed from Lua {"ls", "-l"} ) 等于2(从Lua {"ls", "-l"}传递的字符串数)
plus 1 (last element in the array is actually a zero-terminator). 加1(数组中的最后一个元素实际上是零终止符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM