Warning
Pointer arithmetic is powerful but should be used with caution to avoid undefined behavior. Always ensure that the pointer arithmetic stays within the bounds of the allocated memory.
Pointer.inc([pos]) method
Increments the Pointer memory position to one or more position, meaning it moves by the size of the data type it points to.
Parameters
[pos]
An optional number
, indicating the number of position to increment the pointer, defaulting to 1
position.
Return value
The function returns the incremented Pointer for convenience.Example
local c = require "c"
-- Creates a pointer to a C string
local str = c.string("C FFI module")
-- Creates a Pointer to this string
local pstr = c.Pointer(str)
for i=0,#str-1 do
-- Print the pointer content as a string
print(pstr.content)
-- and increment the string pointer by one char position
pstr:inc();
end