Hi Im new to this comunity and Im in throuble with C heheh. My problem is that:
Im using LUA to create scripting capabilities do my software in C. So, I ve created a file called script.c to suport all script code of my project. See:
script.c (some lines)

int  script_start(lua_State *L)
{
	L = lua_open();
	if (L == NULL)
	{
		script_error(L, "%s", lua_tostring(L, -1));
		return -1;
	}

	luaopen_base(L);
	luaopen_table(L);
	luaopen_io(L);
	luaopen_string(L);
	luaopen_math(L);
	luaopen_debug(L);

	return 0;
}
int script_end(lua_State *L)
{
	lua_close(L);
	return 0;
}

As you can see, the this function starts the script engine on the environment parameter L. On the main program I call this function using this form:
Program test.c

int main(int argc, char *argv[])
{
	lua_State *L;
	script_start(L);
	script_end(L); //error here (accces violation)
	return 0;
}

All seems to work correctly but Im having acess violation using the L pointer after initializing it. Even on script_end(L) function is not working.
My supositions is that I cant send a empty pointer to be filled on the script_start() funcion. Im thinking right?
All help will be apprecied. (Im Brazilian so, sorry about my english)
thanks in advance

Recommended Answers

All 2 Replies

You have to pass to script_start a pointer to a pointer (two stars, not one) so that it can change it

int  script_start(lua_State **L)
{
	*L = lua_open();
	if (*L == NULL)
	{
		script_error(L, "%s", lua_tostring(L, -1));
		return -1;
	}

	luaopen_base(*L);
	luaopen_table(*L);
	luaopen_io(*L);
	luaopen_string(*L);
	luaopen_math(*L);
	luaopen_debug(*L);

	return 0;
}

And see the two changes here too

int main(int argc, char *argv[])
{
	lua_State *L = NULL;
	script_start(&L);
	script_end(L); //error here (accces violation)
	return 0;
}

>>script_error(L, "%s", lua_tostring(L, -1));
When *L == NULL how do you possibly expect that line to work??

Thanks a lot its was exactly the problem. All worked fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.