How can I create a multi-dimensional table in C?

See this thread on the Lua mailing list. We have a C matrix V, which is an array of row arrays, and wish to create the Lua equivalent.

int i,j;
lua_createtable(L , nRows, 0); // push the main table T onto the stack for (j = 1; j <= nRows; j++ ) {
 lua_createtable(L , nCols, 0); // push a Row Table R onto the stack
 for ( i = 1; i <= nCols; i++ ) {
 lua_pushnumber(L, V[j][i]);
 // value is at -1, R is at -2
 lua_rawseti(L, -2, i); // R[i] = V[j][i]
 }
 // R is at -1, T is at -2
 lua_rawseti(L, -2, j); // T[j] = R
}



Back