Sign in to follow this  

if (char* == "SomeText") not working

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

I am trying to compile Domination Game from Eddie Long but have strange situation in debugger after success build: construction 'if (char* == "SomeText")' not working In Watch window i see +game->GetGameName()0x0048a84c "Deathmatch" char * but "if" ignore it and skip all. Why? class GameType {... public: char* GetGameName(){return gameName;}; ... private: char* gameName; ... } class TeamInfo { ... private: GameType *game; ... } ..... gameName="Deathmatch"; ..... if(game->GetGameName() == "Domination") { newAgent = new Agent(firstAgent,id,teamType,DOM_AGENT); } else if(game->GetGameName() == "Deathmatch") { newAgent = new Agent(firstAgent,id,teamType,DEATHMATCH_AGENT); } else if(game->GetGameName() == "CaptureTheFlag") { newAgent = new Agent(firstAgent,id,teamType,CAPTURETHEFLAG_AGENT); } else if(game->GetGameName() == "LastManStanding") { newAgent = new Agent(firstAgent,id,teamType,LASTMANSTANDING_AGENT); } ...

Share this post


Link to post
Share on other sites
This is one good reason to avoid char pointers for text. The == operator compares the pointers themselves, not the contents of the string. You need a function like strcmp() for that. Alternaively, you could make life a lot easier for yourself and use std::string instances to represent text:

#include <string>

class GameType
{
public:
const std::string &GetGameName(){return gameName;};

private:
std::string gameName;
};

// later

gameName = "Deathmatch";

if(game->GetGameName() == "Domination")
{ newAgent = new Agent(firstAgent,id,teamType,DOM_AGENT);
}
else /* ... */

Share this post


Link to post
Share on other sites
If I was you I would change all instance of char* to string. If you ever need the array of chars you can always call the c_str() method on a string type. By changing char* to string will also fix your current dilema

Share this post


Link to post
Share on other sites
Sure! When i change '==' to strcmp() all goes OK.
BUT! this code should be correct!
His author Eddie Long have hundreds of this conditions thru all code.
Does it mean that it was possible in VS 2005 or earlier?

Share this post


Link to post
Share on other sites
Quote:
Original post by OlegT
Sure! When i change '==' to strcmp() all goes OK.
BUT! this code should be correct!
His author Eddie Long have hundreds of this conditions thru all code.
Does it mean that it was possible in VS 2005 or earlier?

No, you have always needed to use strcmp or similar functions to compare strings in C. If the code you are talking about really is comparing char-pointers, then it is incorrect (if the purpose of the code was to compare strings).

But since you are using C++, you should use std::string instead as many others have already have told you. You are far less likely to do something wrong with a std::string compared to a char-pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by OlegT
Sure! When i change '==' to strcmp() all goes OK.
BUT! this code should be correct!
His author Eddie Long have hundreds of this conditions thru all code.
Does it mean that it was possible in VS 2005 or earlier?


operator== has never been the correct way of comparing char* strings for equality. I don't know who Eddie Long is, but I would question if you should really be looking at his code for whatever it is you're trying to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by OlegT
Sure! When i change '==' to strcmp() all goes OK.
BUT! this code should be correct!
His author Eddie Long have hundreds of this conditions thru all code.
Does it mean that it was possible in VS 2005 or earlier?


static const char * a = "A";
static const char * b = "B";
static const char * c = "C";

int main(int argc, char* argv[])
{
const char * p;
switch (argc) {
case 0 : p = a; break;
case 1 : p = b; break;
case 2 : p = c; break;
default : p = NULL;
}

if (p == a) printf(a);
if (p == b) printf(b);
if (p == c) printf(c);
if (p == NULL) printf("NONE");
}


Oh yea, don't do that.

Same thing is possible in Java using .intern(). But only because Sun chose a certain string pooling strategy.

If you need something like this, then it's better to just use ints as string identifiers. Something like:
struct StringRef {
StringRef(const char * p) {
static int counter = 0;
unique = counter;
counter++;
}
int unique;

const char *p;
// or
std::string * p;

};
Then implement custom comparison operators or something. Relying on addresses is simply too fragile, and suffers from other static annoyances.

Share this post


Link to post
Share on other sites

This topic is 3293 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.

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