Jump to content
  • Advertisement
Sign in to follow this  
TheOne1

empty strings....

This topic is 5101 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

SomeClass::SomeClassConstructor()
{
    string = new char[ 50 ];
}

SomeClass::SomeClassgetString()
{
    if ( string == 0 )
       cout << "String is empty";
    else
       cout << string << endl;
}


I did something like the example above, but even though I never assigned string with anything except the new char[ 50 ], it skips ( string == 0 ). How come string is not considered empty?

Share this post


Link to post
Share on other sites
Advertisement
you can not test if a string is empty by comparing it 0. Try using the int strlen(char*) function. example:

if(strlen(str) == 0)
// str is empty


for a little more of an explination:
when you define a char* and set it to new, you give that variable (pointer) a value which is the memory adress of that new memory. when you do if(str == 0), you are testing to see if that memory's address is 0.

Hopefully that made some sence... You might want to do some reasearch on pointers for more info.

Dwiel

Share this post


Link to post
Share on other sites
hmmm. I tried that, but it doesn't work....
this is what I have:

void viewChar( PlClient &seeObj )
{
if ( strlen( seeObj.plName ) == 0 )
cout << "A name is not assigned\n";
else
cout << seeObj.plName << endl;
}


This is what happens:

Share this post


Link to post
Share on other sites
I don't know how it works in C++, but in C when you declare a variable, it always hold something. The best way to ensure a string to be empty is to assign an empty string("") or NULL.

Share this post


Link to post
Share on other sites
ahh. finally I got it. I declared plName as a NULL like:

*plName = NULL;

if I left it as

plName = NULL;

would that mean I'm telling plName to have no address? but when I do: *plName = NULL, I'm telling it to be an empty string?

Share this post


Link to post
Share on other sites
For one thing, after you allocate new memory (with "new char[50]" in your case), the contents are not zero; they're just random garbage. For another, even if the string was empty, the easiest way to check is this:

if( string[0] == 0 )
cout << "string is empty" << endl;

Note that this assumes 'string' has been allocated. If, on the other hand, the string pointer is NULL (not allocated), which is not the same as being a valid, empty, string, then you'd use your original code "string == 0".

Anyway, after you create the string, add this:

string[0] = 0;

That sets the first character to 0. Strings are null-terminated so calling cout on it now will print nothing; it's an empty string.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOne1
ahh. finally I got it. I declared plName as a NULL like:

*plName = NULL;

Which, if plName hasn't been allocated yet, would usually cause a crash, or a memory error that causes the program to crash later on.
Quote:

if I left it as

plName = NULL;

would that mean I'm telling plName to have no address?

Yes.
Quote:
but when I do: *plName = NULL, I'm telling it to be an empty string?

Yes, that's the same as saying 'plName[0] = NULL', but this only works if the string has already been assigned memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOne1
This is what happens:


Simple, the memory you allocate is not initialised to zero and contains random gibberish. Add a "strcpy (plName, "");" to the constructor.

Better yet, simply use std::string, as suggested by Zahlman.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!