// 创建二维数组Lua表
void createLuaTable2(lua_State *L, const char* t)
{
lua_newtable(L); // t
//========================
lua_pushinteger(L, 1); // t[1]
lua_newtable(L); // p1
// p1[1] = 100
lua_pushinteger(L, 1);
lua_pushinteger(L, 100);
lua_settable(L, 3);
// p1[2] = 200
lua_pushinteger(L, 2);
lua_pushinteger(L, 200);
lua_settable(L, 3);
// p1[3] = 300
lua_pushinteger(L, 3);
lua_pushinteger(L, 300);
lua_settable(L, 3);
// t[1] = p1
lua_settable(L, 1);
//==========================
lua_pushinteger(L, 2); // t[2]
lua_newtable(L); // p2
// p2[1] = 'AAAA'
lua_pushinteger(L, 1);
lua_pushstring(L, "AAAA");
lua_settable(L, 3);
// p2[2] = 'BBBB'
lua_pushinteger(L, 2);
lua_pushstring(L, "BBBB");
lua_settable(L, 3);
// p2[3] = 'CCCC'
lua_pushinteger(L, 3);
lua_pushstring(L, "CCCC");
lua_settable(L, 3);
// t[2] = p2
lua_settable(L, 1);
//=========================
lua_setglobal(L, t); // 设置全局变量
}
/*
createLuaTable2(L, "aaa"); 等价于Lua代码:
-- 全局变量
aaa = {
{100,200,300},
{"AAAA", "BBBB", "CCCC"}
};
*/
在C中创建二维Lua表示例
最新推荐文章于 2021-05-22 06:08:04 发布
这段代码展示了如何在C语言中创建一个二维Lua表。通过lua_newtable函数,首先创建了一个新的表`t`,然后在该表下分别创建了两个子表`p1`和`p2`,每个子表包含三个元素。最后,将这两个子表作为`t`的键值对设置,并将整个表格设为全局变量。

1755

被折叠的 条评论
为什么被折叠?



