Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a string from lua file to c++

Tags:

c++

string

char

lua

I am trying to get a string from lua into a char in c++. But for some reason it returns either a pointer or a number. I can use all the help I get on this one. Here is a example of what i'm trying to do:

Lua file:

logo = "ad.png"

C file:

lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"fuel.lua");
const char* logoX;

lua_getglobal(L, "logo");
if(lua_isnil(L,-1)){
    printf("is nil!\n");
}
else{
    logoX = lua_tostring(L, -1);
    printf("the logo is %d\n", *logoX);
}

But this is printing out text that says "the logo is 105". Any help is appreciated. Thank you!

like image 488
0xSingularity Avatar asked Dec 31 '25 16:12

0xSingularity


1 Answers

The problem is in your printf statement:

printf("the logo is %d\n", *logoX);

You are using an integer format string (%d), and you are sending a character argument (*logoX which is the first character of your string). So the printed value is the string's first character integer value.

To make it work, change the printf statement to:

printf("the logo is %s\n", logoX);
like image 74
BlackDwarf Avatar answered Jan 02 '26 09:01

BlackDwarf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!