logical operations for const char *

Jul 19, 2011 at 6:31am
I have a simple question! why is this not true?

(d == "cplusplus")

when

const char *d = "cplusplus";


how should I do the comparison then?
Jul 19, 2011 at 7:09am
Use strcmp() to compare the contents of C-strings. The reason it doesn't work is because they are pointers, and you are comparing the addresses, basically, not the contents of the strings.
Jul 19, 2011 at 2:00pm
Thanks a lot Zhuge.
Jul 19, 2011 at 2:58pm
You can also check it using
*d = "cplusplus";
rather than d.
This worked for me atleast!
Jul 19, 2011 at 3:40pm
it's strange, is it true that if we didn't use it for cout then it used as pointer (address) ? e.g:

1
2
3
4
char sentence [] = "new ones";

if (sentence == "new ones")
cout << "succeded\n";


it doesn't work. but when i do this:

 
cout << sentence << endl;


prints: new ones
Jul 19, 2011 at 3:45pm
Yeah, I noticed that too (Just today as a matter of fact) when trying to make a program that would check the equality of two strings (The actual problem was that I had to do that in the primitive compiler, Turbo C++ [Borland])
Jul 19, 2011 at 4:00pm
so this is true?

is it true that if we didn't use it for cout then it used as pointer (address) ?
Jul 20, 2011 at 6:54am
is this a "yes"?
Jul 20, 2011 at 8:17am
Apparently!
Topic archived. No new replies allowed.