Closed fastcoding closed 2 years ago
--string
local ffi = require"ffi"
t0=ffi.typeof('char []')
p=ffi.new('char[?]',1)
print(ffi.istype(t0,p),p) -- output true
------------------------------
--string array
-- keep strings assigned in anchors to avoid __gc
local anchors = {}
local strs = {"one","two","three"}
local Items = ffi.new("const char*[?]",#strs)
for i = 0,#strs-1 do
anchors[#anchors+1] = ffi.new("const char*",strs[i+1])
Items[i] = anchors[#anchors]
end
print(Items)
thanks a lot, the [?] solved my issue!
Some functions with array argument is not callable through generic version due to its ffi ctype is invalid, for example,
Creating any instance of array with unknown size from ffi will lead to error, for example,
How about replace all [] with *?