lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Apr 7, 2019 at 4:05 AM Marc Balmer <> wrote:
So far I came up with two different ways:

First variant:

        const char *s;
        char *u;

        s = luaL_checklstring(L, 2, &len);
        u = lua_newuserdata(L, len);
        memcpy(u, s, len);

If you plan to pass a string to C string routines you have to copy the null character.

u = lua_newuserdata(L, len+1);
memcpy(u, s, len+1);

I assume the Lua string does not contain any null characters, or the C routines will stop processing in the middle of a Lua string. If you want to process null characters as part of strings you may have to write your own code.