Created
August 13, 2014 19:50
-
-
Save markuman/732b6b934f29393dc633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <lua.h> | |
#include <lualib.h> | |
#include <lauxlib.h> | |
int main() { | |
int result; | |
lua_State *L = luaL_newstate(); | |
luaL_openlibs(L); | |
// a = {7, 6, 8, 3, 4, 1} | |
result = luaL_loadfile(L, "array.lua") || lua_pcall(L, 0, 0, 0); | |
if (result){ | |
fprintf(stderr, "Fehler beim Laden des Skripts: %s!\n", lua_tostring(L, -1)); | |
} | |
lua_getglobal(L, "a"); | |
// length of array | |
lua_len(L, 1); | |
int len = lua_tonumber(L, -1); | |
printf("%d\n",len); | |
// double a[6] = {}; | |
double *a = (double*)malloc(len*sizeof(double)); | |
for(int n = 0; n < len; n++) { | |
lua_pushnumber(L, n+1); | |
lua_gettable(L, -2); | |
a[n] = lua_tonumber(L,-2); | |
} | |
// Close the Lua state | |
lua_close(L); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment