0

I have a definition as follows:

#define namef (s, r, x) (p_name ((s), (r), (x)))

My file lua is follows:

tbl= {
    name_func = module;
};

my code is as follows:

void getname(void) {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    char *arc = "luafun.lua";


    if (luaL_dofile(L, arc)) {
        printf("Error in %s", arc);
        return;
    }

    lua_getglobal(L, "tbl");
    lua_getfield(L, -1, "name_func"); 
    namef(r_name, lua_tostring(L, -1), sizeof(r_name)); 

    lua_close(L);
    printf("done");
}

r_name is an array char r_name [11];

but it is giving the following error:

PANIC: unprotected error in call to Lua API (attempt to index a nil value)

I don't know why this is occurring, in C works normally, more to change to lua error occurs

Chozie
  • 73
  • 1
  • 9

1 Answers1

2

First, you're posting a lot of stuff that's totally unrelated to the problem. We don't need to see p_name or your macro or anything that's not related to the Lua error. See: volume is not precision. As part of trouble shooting this problem yourself, you should have removed extraneous things until you had the smallest snippet that reproduced the problem. You would have ended up with something like this:

  lua_State *L = luaL_newstate();
  if (luaL_dofile(L, lua_filename)) {
     return;
  }
  lua_getglobal(L, "tbl");
  lua_getfield(L, -1, "name_func"); 

The problem is that your file is not being found. Per the manual, luaL_dofile returns true if there are errors and false if there are no errors.

Your program aborts if the file is found. Only if the file isn't found does it try to index tbl, which it can't, because that global variable doesn't exist.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • It was a mistake when editing the code, the function of reading is correct. But the file is being read, I did a debug and is pointing error here `namef(r_name, lua_tostring(L, -1), sizeof(r_name)); ` – Chozie Nov 05 '15 at 05:55
  • 1
    So the code you're asking us to troubleshoot isn't even the code with the problem? Show the actual code. The error definitely isn't on the `namef` line, because there's no code there that attempts to index a Lua table. – Mud Nov 05 '15 at 06:09