gpt4 book ai didi

c - 访问从 Lua 到 C 的嵌套表以获取值

转载 作者:行者123 更新时间:2023-12-04 10:44:52 25 4
gpt4 key购买 nike

您好,我正在尝试访问从 Lua 发送到 C 的嵌套表。该表是:

arg =
{
MagicNumber = {MagicNumber, 0},
ProdNum = {ProdNum, 1},
LetterR = {LetterR, 0xc},
Revision = {Revision, 0xd},
Space1 = {Space1, 0xe},
MnfctrCode = {MnfctrCode, 0xf},
Hyphen1 = {Hyphen1, 0x12},
ZeroCode = {ZeroCode, 0x13},
Hyphen2 = {Hyphen2, 0x15},
MnfctrMnth = {MnfctrMnth, 0x16},
MnfctrYear = {MnfctrYear, 0x18},
SerialNum = {SerialNum, 0x1a},
Space2 = {Space2, 0x1e},
ChkSum = {ChkSum, 0x1f},
}

里面的表都是整型值,表中的key是字符串。我的代码片段如下:

lua_pushnil(L);

while(lua_next(L, -2) != 0)
{
field = lua_tostring(L, -2);
printf("\n %d field = %s", i, field);
wrData[i-1] = lua_tonumber(L,-1);
printf("\n data = 0x%x", wrData[i-1]);
lua_pop(L, -1);
i++;
}

我是否遗漏了什么,因为我得到的值是 0x0

最佳答案

根据您的需要更改此函数,并使用堆栈顶部的表调用它。

   void luaAccess(lua_State * L)// call this function with the table on the top of the stack
{
lua_pushnil(L);
while(lua_next(L, -2))
{
switch(lua_type(L, -2))
{
case LUA_TSTRING:
//deal with the outer table index here
break;
//...
// deal with aditional index types here
}
switch(lua_type(L, -1))
{
case LUA_TTABLE:
lua_pushnil(L);
while (lua_next)
{
switch(lua_type(L, -2))
{
case LUA_TNUMBER:
// deal with the inner table index here
break;
//...
// deal with aditional index types here
}
switch(lua_type(L, -1))
{
case LUA_TSTRING:
//deal with strings
break;
case LUA_TNUMBER
//deal with numbers
break;
}
lua_pop(L, 1);
}
//...
// deal with aditional index types here
break
}
}
lua_pop(L, 1);
}

关于c - 访问从 Lua 到 C 的嵌套表以获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41387796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com