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:dec([pos]) method
Decrements 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 decrement the pointer, defaulting to 1
position.
Return value
The function returns the decremented Pointer for convenience.Example
local str = c.string("C FFI module")
-- Creates a Pointer to this string
local pstr = c.Pointer(str)
local inv = c.Pointer(str)
inv = inv + #str-1
for i=#str-1,0, -1 do
-- set the inv.content (a char) from the pstr.content
inv.content = pstr.content
-- Print the inv pointer as a string
print(inv)
-- and increment the string pointer by one char position
inv:dec();
end