Warning
Casting pointers can be dangerous if not done carefully, leading to potential undefined behaviour.
Pointer:as(type) method
Casts the current Pointer to the provided Pointer type.
Parameters
type
A value indicating the new pointer type, any of the following values :
string
that indicates the primitive C pointer type to be casted to :
C primitive pointer strings |
---|
unsigned char* , char* , wchar_t* , bool* , short* , unsigned short* |
int* , unsigned int* , long* , long long* |
unsigned long* unsigned long long* size_t* int16_t* int32_t* |
int64_t* , uint16_t* , uint32_t* , uint64_t* , float* , double* |
string , void* , wstring | code>
Return value
The function returns a new Pointer pointing to the same memory address but casted to the provided C type.Example
local c = require "c"
-- Creates a Pointer to a Buffer in memory (Pointer of type "void*")
local buff = sys.Buffer("\x48\x65\x6C\x6C\x6F\x20\x4C\x75\x61\x52\x54\x20\x21\x00")
local c_ptr = c.Pointer(buff)
-- always false : we are comparing a memory pointer to a Lua string value
print(c_ptr == "Hello LuaRT !")
-- always true : converts the memory pointer to a Lua string value
local lua_string = tostring(c_ptr:as("string")) -- c_ptr is casted to a "string" before
print(lua_string == "Hello LuaRT !")