0Pricing
Lua Academy · Lesson

Pointers, Structs, and Arrays in FFI

Work with C pointers, struct types, and array allocations in FFI.

C Types in FFI

FFI supports all standard C types: int, float, double, char, pointers, arrays, and structs. Declare them in ffi.cdef.

Creating C Arrays

Allocate a C array with ffi.new("type[N]"). Access elements with 0-based indexing (C convention).

local ffi = require("ffi")
local arr = ffi.new("int[5]", {10, 20, 30, 40, 50})
print(arr[0], arr[1], arr[4])  -- 10  20  50

All lessons in this course

  1. FFI Basics: ffi.cdef and ffi.load
  2. Calling C Functions via FFI
  3. Pointers, Structs, and Arrays in FFI
  4. Callbacks: Passing Lua Functions to C
← Back to Lua Academy