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

Started by
8 comments, last by Kylotan 15 years, 4 months ago
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); } ...
Advertisement
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;};// latergameName = "Deathmatch";if(game->GetGameName() == "Domination"){	newAgent = new Agent(firstAgent,id,teamType,DOM_AGENT);}else /* ... */
You cannot use == to compare char* 'strings' in C or C++, because it tests pointer equality, not 'string' equality.

In C, use strcmp.
In C++, use std::string (and then you can use operator==).
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
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?
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.
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.
Thanks to all. rip-off your code helps 100%!
Without modification all conditions in code i just change type from char* to std::string i successfully start this game! Thanks a lot!
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.
I think it's quite easy in many compilers to force it to put all occurrences of a constant string into the same bit of memory so that it would have worked. But it's really not the 'right' thing to do.

This topic is closed to new replies.

Advertisement