Pointers, Structs, and Arrays in FFI
Work with C pointers, struct types, and array allocations in FFI.
Pointers, Structs, and Arrays in FFI is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 50Pointers
Create a pointer to a value or allocate pointer types. Use ptr[0] to dereference.
local ffi = require("ffi")
local p = ffi.new("int[1]", 42) -- int* pointing to 42
print(p[0]) -- 42
p[0] = 99
print(p[0]) -- 99Struct Definition and Access
Declare a struct in cdef, then create instances with ffi.new and access fields with dot notation.
ffi.cdef[[
typedef struct {
float x, y, z;
} Vec3;
]]
local v = ffi.new("Vec3", 1.0, 2.0, 3.0)
print(v.x, v.y, v.z)Array of Structs
Create arrays of structs for bulk data processing — common in game engines and numerical computing.
local particles = ffi.new("Vec3[1000]")
for i = 0, 999 do
particles[i].x = i * 0.1
particles[i].y = 0
particles[i].z = 0
endPointer Arithmetic
Advance a pointer with ptr + n. This returns a new pointer offset by n elements (not bytes).
local arr = ffi.new("int[5]", {1,2,3,4,5})
local p = arr + 2 -- pointer to arr[2]
print(p[0]) -- 3ffi.cast: Type Casting
ffi.cast(ctype, val) reinterprets a value as another C type. Use carefully — wrong casts cause undefined behavior.
local ffi = require("ffi")
local i = ffi.cast("int32_t", -1)
print(i) -- -1
local u = ffi.cast("uint32_t", i)
print(u) -- 4294967295ffi.sizeof and ffi.alignof
ffi.sizeof("Vec3") returns the byte size of a C type. ffi.alignof returns its alignment requirement.
ffi.gc: Attaching Finalizers
ffi.gc(ptr, cleanup) attaches a Lua function to a C pointer so it is called when the pointer is GC'd — enabling RAII for C memory.
local p = ffi.gc(ffi.C.malloc(1024), ffi.C.free)
-- p is automatically freed when GC collects itNull Pointer Checks
Compare pointers to nil (which equals the C NULL pointer in FFI comparisons).
local ptr = lib.allocate()
if ptr == nil then error("allocation failed") endPerformance of C Arrays
C arrays accessed through FFI are significantly faster than Lua tables for numeric data — no boxing, no hash lookups, no type checks.
FFI Arrays Question
What index does FFI use for C arrays?
Recap: Pointers, Structs, Arrays in FFI
Create C arrays with ffi.new("type[N]") (0-indexed), access struct fields with dot notation, use ffi.cast for type reinterpretation, and ffi.gc for automatic C memory cleanup.
Frequently asked questions
Is the “Pointers, Structs, and Arrays in FFI” lesson free?
Yes — the full text of “Pointers, Structs, and Arrays in FFI” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Pointers, Structs, and Arrays in FFI”?
Work with C pointers, struct types, and array allocations in FFI. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Pointers, Structs, and Arrays in FFI” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- FFI Basics: ffi.cdef and ffi.load
- Calling C Functions via FFI
- Pointers, Structs, and Arrays in FFI
- Callbacks: Passing Lua Functions to C