Sign in to follow this  
CodaKiller

How can I figure out if this char array is empty?

Recommended Posts

Well they char array is coming from a lua function and when I try to check if it's empty like this: char* filename = (char*)lua_tostring(L, 3); if( filename != "" ) { // Do stuff here. } It always thinks the char array is full but all I did was pass "" to the lua function, how can I do this?

Share this post


Link to post
Share on other sites
You cannot compare char* and expect string comparisons. char* is a pointer, not a string, it's just often interpreted as string data in C and C++. It still behaves like what it is, however: a pointer.

In C, use the strcmp() function to do string comparisons with char* objects. In C++, construct a std::string from the char*, which will allow you to do natural operator== tests for string equality.

Share this post


Link to post
Share on other sites

char* filename = (char*)lua_tostring(L, 3);
if ( filename && *filename )
{
// Do stuff here.
}

The filename inside the if assures that the pointer isn't null, and the *filename assures that the first character pointed to isn't the null terminator.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this