3

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!

1
  • You use %d in your printf, shouldn't that be %s? Commented Jul 26, 2014 at 1:05

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.