Sign in to follow this  
siliconsurfer

Comparing Char's the right way C++

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites

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