简体   繁体   中英

Lua table of string values as parameter in C function

I want to make a C function that takes a lua table with strings as parameter, and the lua table does not have any keys, just values. How can I do this? I cannot figure it out. I did not find anything when I searched in google.

The "default" keys in a table are consecutive integers starting from 1. This:

{"hello", "world"}

is the same as:

{[1] = "hello", [2] = "world"}

You cannot access these entries with lua_getfield , because that takes a string key. You can do it the "manual" way, with lua_pushnumber and lua_gettable . If L is your lua_State* , t is the index of the table on the stack and and k is the key, then:

lua_pushnumber(L, k);
lua_gettable(L, t);

should do the same thing as:

lua_getfield(L, t, k);

does for string keys. Note that if t is a relative index (a negative number), then because you're pushing another item onto the stack, you'll need to adjust it by 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM