I'm reading over the C API Lua documentation, and I notice in this code:
lua_pushnil(L); /* First key */
while (lua_next(L, t) != 0) {
/* Uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
/* Removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
It looks normal, apart from the lines where it uses -2 and -1 for the stack indexes.
I tried changing them to positives, and it didn't seem to have any affect on the program. Why would they specifically use negatives? Why doesn't this cause a compiler error? I'm sure you can't use unsigned ints with negative numbers.
I don’t know if it makes any changes, but I'm using C++ instead of C, so maybe that has something to do with it?
According to the documentation: