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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
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.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “Pointers, Structs, and Arrays in FFI” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “Pointers, Structs, and Arrays in FFI”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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