lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I don't understand. Don't values get pushen onto the bottom of the stack?? Or are tables different? In that case wouldn't the table be at -1?? When I call 'C' functions from lua with numbers as parameters, I can find them on the bottom of the stack. This is confusing.

From: "Peter Prade" <>
To: Multiple recipients of list <>
Subject: RE: Lua Tables from 'C'
Date: Mon, 15 Oct 2001 03:03:57 +0200

Hi Denis,

try lua_gettable(L, -2)

the table is not #2 on the stack, but the second from the top!
(positive numbers indicate absolute position from the bottom of the stack,
negative a position relative to the top)
see also Chapter "5.2 - The Stack and Indices" in the manual:


-----Original Message-----
[]On Behalf Of Denis Lamarche
Sent: Monday, October 15, 2001 2:15 AM
To: Multiple recipients of list
Subject: Lua Tables from 'C'

I am trying to get the value in a table passed to 'C' from lua.

names={car="red", house="brick"}

static int doit(lua_State *L)
char *text;

lua_pushstring(L, "house");
lua_gettable(L, 2)  //because the table is at 2 in the stack right?
text=(char *)lua_tostring(L, 1);


but for some strange reason, text is NULL.  I am assuming that the table
names was passed on the stack as index 1 and then I pushed a string on the
stack, so the table is now index 2, right?? but the string "brick" does not
get pushed onto the stack.

I tried to lua_getglobal before the pushstring to try to get the table, but
I dont know how to getglobal the table that is being sent by lua.

any thoughts?

Get your FREE download of MSN Explorer at

Get your FREE download of MSN Explorer at