Comparing Char's the right way C++

Started by
9 comments, last by Shannon Barber 18 years, 6 months ago
Hi, I know you shouldn't use the "==" operator to compare to strings (or char *)in C++, afterall thats what strcmp is for. But I was wondering is it valid to compare to char's using "=="? Cheers
Advertisement
you can compare chars with ==

in c/c++ a char is just a small integer.

with c++ there is a much nicer alternavie to char* and strcmp(),

std::string

you can use == on it
Yes, for comparing a single char to another single char, == is perfectly valid. The reason you cannot use it on char* is that that will compare pointer values rather than the contents of the strings. Anyway, if you're using C++ you shouldn't use char* anyway. You should use std::string instead.

Tom

EDIT: predictably someone else beat me to it :)
Also, if you used -> to dereference the pointer and used == to compare the values, it would only get the first char in the array, not the whole string. Using the std:string class thingy would probably be better because it overloads the == operator to compare the two strings. Yes, I saw that this was stated above, but I wanted to repeat to state my own opinion as well. I can say that I haven't needed to use the string class myself yet simply because I don't ever need to compare strings, rather I use them to print text for OpenGL. For someting simple like this, I don't recommend use of the class.


umm, * is the derefernce operator

dunno how you would use -> with a char*

In a class, maybe?
struct foo{    foo() {bar = new char('b');}    ~foo() {delete bar;}    char *bar;};int main(){    foo foobar;    char foo2 = foobar->bar;    return 0;}


This code might and might not work. Its simply a hypothesis using info from many people and libraries.
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
Quote:Original post by dudedbz1
In a class, maybe?
struct foo{    foo() {bar = new char('b');}    ~foo() {delete bar;}    char *bar;};int main(){    foo foobar;    char foo2 = foobar->bar;    return 0;}


This code might and might not work. Its simply a hypothesis using info from many people and libraries.


It will store the address of what bar is pointing to in foo2 (well, probably just part of it since a char is usually a byte and most modern machines have at least a 32-bit word size and it would be pretty much useless to store part of it), I doubt that's what you're going for.

EDIT:
In response to the original question: Yes, you can compare chars with the == operator. The reason you can't compare char pointers that way (well, you can but it probably won't do what you expect) is because it would compare the actually memory addresses of the two arrays not the values since a char* is a pointer.
Quote:Original post by dudedbz1
In a class, maybe?
struct foo{    foo() {bar = new char('b');}    ~foo() {delete bar;}    char *bar;};int main(){    foo foobar;    char foo2 = foobar->bar;    return 0;}



yeah but that isnt what was suggested

Quote:Original post by kburkhart84
if you used -> to dereference the pointer and used == to compare the values, it would only get the first char in the array, not the whole string.


Quote:Original post by dudedbz1
*** snippet snipped ***

Lots of troubling things in that code; don't want anybody to be lead astray - let me try and clarify:
struct foo;int main() {   // create an object and a pointer to foo   foo foo_object;               // instanciate a foo object   foo * foo_pointer( new foo ); // point to a newly created foo object   // accessing a foo object   char c( * foo_object.bar );   // initialize a char with object at foo's bar   char * cp( foo_object.bar );  // initialize a char pointer with foo's bar   // accessing a foo object through a pointer   c = * foo_pointer->bar;        // assign object at foo's bar   cp = foo_pointer->bar;         // assign foo's bar   delete foo_pointer;            // be nice :)   return 0;}struct foo {   foo(): bar( new char('b') ) {;}   ~foo() { delete bar; }      char * bar;};
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
I know! That guy said that the * was for dereferencing and that the -> was for he had no idea. I took an educated guess.
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------

This topic is closed to new replies.

Advertisement