Lua API的lua_tostring()处理布尔值时在C程序中返回null的问题求助
Lua API的lua_tostring()处理布尔值时在C程序中返回null的问题求助
我最近碰到个棘手的问题:在C程序里调用Lua API的lua_tostring()函数处理布尔值时,返回的居然是(null),但在Lua 5.4.2的交互式shell里做同样的操作却完全正常,能正确输出true,我实在搞不懂这中间的差异到底是怎么回事。
下面是我写的完整测试代码,方便大家帮我排查问题:
// Compiled on windows with: cl .\test_luatostring.c ..\lua54.lib // Tested with lua-5.4.2_Win32_vc17_lib #include <stdio.h> #include <lua.h> #include <lauxlib.h> #include <lualib.h> int main() { lua_State *L = luaL_newstate(); luaL_openlibs(L); // 将布尔值true压入Lua栈 lua_pushboolean(L, 1); // 尝试把栈顶的布尔值转换为字符串 const char *str = lua_tostring(L, -1); // 这里实际输出的是(null),和预期不符 printf("Result: %s\n", str); lua_close(L); return 0; }
补充说明:我是在Windows环境下编译的,用的编译命令是cl .\test_luatostring.c ..\lua54.lib,测试用的Lua库是lua-5.4.2_Win32_vc17_lib版本。
备注:内容来源于stack exchange,提问作者Xitog
相关产品推荐
相关产品推荐

