• Advertisement
Sign in to follow this  

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

This topic is 3354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement