empty strings....

Started by
7 comments, last by Kippesoep 19 years, 8 months ago

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?
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
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
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:
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
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.
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?
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
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.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
See, this is why in C++ you should use std::string. Much less confusing. :)
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.
Kippesoep

This topic is closed to new replies.

Advertisement