我在 C 中有一个元素数组(动态),我将作为指针返回。
我需要使用指针读取那些数组元素的值。
是否有任何函数可以从 C 访问指针并在 Lua 中检索值?
最佳答案
您可以将此指针包装到用户数据中并编写访问器方法(复杂性:高)。更简单的解决方案是将该数组转换为常规 Lua 表。
size_t arr_size = 10;
int arr[10] = { 0 };
lua_getglobal(L, "testfunc");
lua_createtable(L, arr_size, 0);
for (size_t i = 0; i < arr_size; i++) {
lua_pushinteger(L, arr[i]);
lua_rawseti(L, -2, i+1);
}
// the table is at top of stack
lua_call(L, 1, 0); // call testfunc(t)
关于c - 如何在 Lua 中访问 C 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16479716/