Skip to main content
GameDev.net gamedev.net
🔒 Locked

Newbie question: Comparing two strings.......

Started by 3DNeophyte Jun 13, 2002 at 11:24 AM 16 replies 400+ views
Original Post
3DNeophyte
3DNeophyte
Ok, in my simple linked list, I''ve got a few functions that require two strings to be compared. Here''s my code: char* a = "Zweihander"; char* b = "Dai-Katana"; char* c = "Zweihander"; if (a == c) { cout << "the two strings match" << endl; } else { cout << "something''s terribly wrong" << endl; } Ok, needless to say, the else statement comes up every time . So, I want to know how to compare two strings. Please reply, as my project is getting slowed down quite a bit due to this quaint roadblock. Thanks
Michalson
Michalson
By using a==c you are comparing the pointer values (the address in memory where the string is being stored), rather than the strings (the characters). You need to use a function like strcmp to compare the actual characters in the string.
powdahound
powdahound
Well doing that aren''t you comparing the memory addresses? Which would never be the same... You''d have to use the string.h function for comparing two c strings. Or you could just switch over and use stl strings. Don''t know a lot about c strings, because I had to learn stupid apstrings in my AP CS class (similar to STL though). I think you want to do if(strcmp(a, c)) or something like that

Gamer-Insight.com
Melraidin
Melraidin
Look up strcmp () on www.cplusplus.com. Also maybe consider using std::string, if you're using C++

[edited by - Melraidin on June 13, 2002 12:31:03 PM]
SabreMan
SabreMan
To compare two c-strings, look up the function strcmp(). However, since you are using C++, you might like to consider using std::string rather than c-strings, which would allow you to say:


  
#include <string>

int main()
{
std::string a = "stuff";
std::string b = "junk";

if(a==b)
//...

}
Melraidin
Melraidin
Oh my.... two posts within 40 seconds of each other, both saying the exact same thing...
Doener
Doener
char a[] = "Something";
char b[] = "Something";

a == b, will be false.
Since a and b are pointers to char and a == b will compare the addresses stored in the pointers. Use strcmp(char*, char*),
it returns 0 if the strings are equal. Return <0, >0 if they differ, don''t know the exact meaning of them both right now.

char *a = "Anything";
-> Bad Idea. a will point to an address where the string is stored hard coded in the source.
3DNeophyte
3DNeophyte
Wow, that''s the fastest I''ve ever seen anyone respond to a question. Anyway, I''ve never really read up on strings, so I had no clue what was goin'' on. Thanks guys, I''ll try the solutions in a little while.

3DNeophyte
3DNeophyte
Ok, so a little while is more like two minutes. Strcmp works like a charm. Again, thanks a lot

smart_idiot
smart_idiot
quote:
Original post by 3DNeophyte
char* a = "Zweihander";
char* b = "Dai-Katana";
char* c = "Zweihander";

if (a == c)
{
cout << "the two strings match" << endl;
}
else
{
cout << "something''s terribly wrong" << endl;
}



Actually, a should equal c because the compiler is supposed to store identical string constants in the same location to save memory.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Promit
Promit
For god''s sake, why don''t you give him the standard C style comparison:

  
char* a = [something];
char* b = [something];

int main
{
if( strcmp( a, b) == 0 )
cout << "The strings are the same";
else
cout << "The strings are different";
}

That''s all he needed.

____________________________________________________________
Direct3D vs. OpenGL
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
SabreMan
SabreMan
quote:
Original post by smart_idiot
Actually, a should equal c because the compiler is supposed to store identical string constants in the same location to save memory.

The results of comparing two pointers from differently allocated blocks of memory is undefined.
bishop_pass
bishop_pass
quote:
Original post by siaspete
If you''ve got string pooling enabled in your options they will probably be the same, but as SabreMan says - it''s not a good idea as it''s undefined

Not only is it not a good ide, it''s not how you compare strings and that could never be taken as a way to compare strings whether you have strings pooled or not.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
ShawnO
ShawnO
quote:
Original post by Promit
For god''s sake, why don''t you give him the standard C style comparison:



Because he is not using C.


When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
smart_idiot
smart_idiot
I never said it was a good idea, I just thought that ''if (a == c)'' should have worked for the wrong reason.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.