C++ 从常量字符创建Lua表**

C++ 从常量字符创建Lua表**,c++,lua,lua-table,C++,Lua,Lua Table,我有一个const char**,它的长度会有所不同,但我想从const char**创建一个Lua数组 我的常量字符**是这样的 arg[0]="Red" arg[1]="Purple" arg[2]="Yellow" 我需要在Lua中将这个数组转换成一个全局表,但我不知道该怎么做,因为我不太擅长处理Lua。intmain() int main() { char* arg[3] = { "Red", "Purple", "Yellow" };

我有一个
const char**
,它的长度会有所不同,但我想从
const char**
创建一个Lua数组

我的
常量字符**
是这样的

arg[0]="Red"
arg[1]="Purple"
arg[2]="Yellow"
我需要在Lua中将这个数组转换成一个全局表,但我不知道该怎么做,因为我不太擅长处理Lua。

intmain()
int main()
{
   char* arg[3] = {
      "Red",
      "Purple",
      "Yellow" };

   //create lua state
   Lua_state* L = luaL_newstate();

   // create the table for arg
   lua_createtable(L,3,0);
   int table_index = lua_gettop(L);

   for(int i =0; i<3; ++i )
   {
      // get the string on Lua's stack so it can be used
      lua_pushstring(L,arg[i]);

      // this could be done with lua_settable, but that would require pushing the integer as well
      // the string we just push is removed from the stack
      // notice the index is i+1 as lua is ones based
      lua_rawseti(L,table_index,i+1);
   }

   //now put that table we've been messing with into the globals
   //lua will remove the table from the stack leaving it empty once again
   lua_setglobal(L,"arg");
}
{ 字符*arg[3]={ “红色”, “紫色”, “黄色”}; //创建lua状态 Lua_state*L=luaL_newstate(); //为arg创建表 lua_createtable(L,3,0); int table_index=lua_gettop(L);
对于(inti=0;iOkay,非常感谢您的回答,这非常简单。我现在了解了表的结构。