title: Read Lua table from C
body: <p>I'm trying to pass a Lua table to my C program but I don´t know how to do it.</p>
  
  <p>My Lua code:</p>
  
  <pre><code>local stages = {}
  stages[1] = stage1
  stages[2] = stage2
  stages[3] = stage3
  
  lstage.buildpollingtable(stages)
  </code></pre>
  
  <p>My C code:</p>
  
  <pre><code>static int lstage_build_polling_table (lua_State * L) {    
      luaL_checktype(L, 1, LUA_TTABLE);
  
      lua_getfield(L, 1, "stage1");
      lua_getfield(L, 1, "stage2");
      lua_getfield(L, 1, "stage3");
  
      stage_t s1 = lstage_tostage(L, -3);
      stage_t s2 = lstage_tostage(L, -2);
      stage_t s3 = lstage_tostage(L, -1);
  
      printf("%d\n",s1-&gt;priority);
      printf("%d\n",s2-&gt;priority);
      printf("%d\n",s3-&gt;priority);
  
      return 1;
  }
  </code></pre>
  
  <p>What do I have to do to run all over the elements? This code generates an error like this:</p>
  
  <blockquote>
    <p>bad argument #-3 to 'buildpollingtable' (lstage-Stage * expected, got table)</p>
  </blockquote>
  
  <p>Can anyone explain what am I doing wrong?</p>