q66 / cffi-lua

A portable C FFI for Lua 5.1+
MIT License
176 stars 24 forks source link

Support passing pointer for array argument #9

Closed vsergeev closed 3 years ago

vsergeev commented 3 years ago
local ffi = jit and require('ffi') or require('cffi')

ffi.cdef[[
    int pipe(int fildes[2]);
]]

--[[
-- also:
local buf = ffi.new('int [2]')
local pipe_fds = ffi.cast('int *', buf)
]]--

local pipe_fds = ffi.new('int [2]')
ffi.C.pipe(pipe_fds) --> expected: no error, get: cannot convert 'int *' to 'int [2]'
print(pipe_fds[0]) --> 3
print(pipe_fds[1]) --> 4

It appears that cdata<T [N]> and cdata<T *> types cannot be passed as an argument to a function expecting T [N].